コード例 #1
0
        public void GetAllPaths(WeightedGraph graph, string startVertex)
        {
            List <List <string> > paths       = new List <List <string> >();
            List <List <int> >    pathWeights = new List <List <int> >();

            List <string> path       = new List <string>();
            List <int>    pathWeight = new List <int>();

            foreach (string v in graph.Vertices)
            {
                if (v.Equals(startVertex))
                {
                    continue;
                }
                path.Clear();
                if (predecessorDict[v] == null)
                {
                    continue;
                }

                string v2 = v;
                while (predecessorDict[v2] != null)
                {
                    path.Add(predecessorDict[v2]);
                    v2 = predecessorDict[v2];
                }

                path.Reverse();
                for (int i = 0; i < path.Count; i++)
                {
                    string v3 = path[i];
                    int    w  = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, v);
                    pathWeight.Add(w);
                }

                paths.Add(path);
                pathWeights.Add(pathWeight);
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            WeightedGraph wg = new WeightedGraph("g.txt");
            Folyed        fb = new Folyed(wg);

            if (fb.hasNegCycle)
            {
                Console.WriteLine("exits negative cycle");
            }
            else
            {
                for (int i = 0; i < wg.V; i++)
                {
                    for (int k = 0; k < wg.V; k++)
                    {
                        Console.WriteLine($"{i}->{k}  : {fb.DistTo(i,k)}");
                    }
                }
                //foreach (var item in fb.Path(3))
                //{
                //    Console.WriteLine(item);
                //}
            }
        }
コード例 #3
0
        public static void CalcShortestPath(WeightedGraph graph)
        {
            Dictionary <string, int>    verticesDict    = new Dictionary <string, int>();
            List <string>               chosenPath      = new List <string>();
            Dictionary <string, string> predecessorDict = new Dictionary <string, string>();

            foreach (string vertix in graph.Vertices)
            {
                if (!vertix.Equals(graph.startVertex))
                {
                    verticesDict.Add(vertix, int.MaxValue);
                }
            }
            verticesDict.Add(graph.startVertex, 0);
            predecessorDict.Add(graph.startVertex, null);

            List <string> queue = new List <string>();

            queue.AddRange(graph.Vertices);
            //queue.Remove(graph.startVertex);

            while (queue.Count != 0)
            {
                string u       = null;
                int    minimum = int.MaxValue;
                foreach (string v in queue)
                {
                    if (verticesDict[v] < minimum)
                    {
                        minimum = verticesDict[v];
                        u       = v;
                    }
                }
                queue.Remove(u);

                foreach (string v in graph.adjacentVertices[u])
                {
                    if (verticesDict[u] + graph.GetWeight(u, v) < verticesDict[v])
                    {
                        verticesDict[v] = verticesDict[u] + graph.GetWeight(u, v);
                        //verticesDict[u] = verticesDict[v]
                        predecessorDict[v] = u;
                    }
                    chosenPath.Add(u);
                }
            }

            List <string> path = new List <string>();

            foreach (string v in graph.Vertices)
            {
                if (v.Equals(graph.startVertex))
                {
                    continue;
                }
                path.Clear();
                if (predecessorDict[v] == null)
                {
                    Console.WriteLine("Do mjesta {0} se ne može doći iz mjesta {1}!", v, graph.startVertex);
                    continue;
                }

                string v2 = v;
                while (predecessorDict[v2] != null)
                {
                    path.Add(predecessorDict[v2]);
                    v2 = predecessorDict[v2];
                }

                string line = "{0} [{1}]: ";
                int    wSum = 0;
                //Console.Write("{0} [{1}]: ", v);
                path.Reverse();
                for (int i = 0; i < path.Count; i++)
                {
                    string v3 = path[i];
                    int    w  = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, v);
                    line += string.Format("{0} --[{1}]--> ", v3, w);
                    wSum += w;
                    //Console.Write("{0} --[{1}]-->", v3, w);
                }

                line += v;
                //Console.WriteLine(v);
                Console.WriteLine(line, v, wSum);
            }
        }
コード例 #4
0
        public void CalcShortestPath(WeightedGraph graph, string startVertex, string endVertex)
        {
            verticesDict.Clear();
            predecessorDict.Clear();
            foreach (string vertix in graph.Vertices)
            {
                if (!vertix.Equals(startVertex))
                {
                    verticesDict.Add(vertix, int.MaxValue);
                }
            }
            verticesDict.Add(startVertex, 0);
            predecessorDict.Add(startVertex, null);

            List <string> queue = new List <string>();

            queue.AddRange(graph.Vertices);

            while (queue.Count != 0)
            {
                string u       = null;
                int    minimum = int.MaxValue;
                foreach (string v in queue)
                {
                    if (verticesDict[v] < minimum)
                    {
                        minimum = verticesDict[v];
                        u       = v;
                    }
                }
                if (u != null)
                {
                    queue.Remove(u);

                    foreach (string v in graph.adjacentVertices[u])
                    {
                        if ((graph.Connected(u, v)) && (verticesDict[u] + graph.GetWeight(u, v) < verticesDict[v]))
                        {
                            verticesDict[v]    = verticesDict[u] + graph.GetWeight(u, v);
                            predecessorDict[v] = u;
                        }
                    }
                }
                else
                {
                    break;
                }
            }


            path.Clear();
            pathWeights.Clear();

            string v2 = endVertex;

            if (predecessorDict.ContainsKey(v2))
            {
                while (predecessorDict[v2] != null)
                {
                    path.Add(predecessorDict[v2]);
                    v2 = predecessorDict[v2];
                }
                path.Reverse();
                for (int i = 0; i < path.Count; i++)
                {
                    string v3 = path[i];
                    int    w  = i != path.Count - 1 ? graph.GetWeight(v3, path[i + 1]) : graph.GetWeight(v3, endVertex);
                    pathWeights.Add(w);
                }
            }
            else
            {
                path.Add(null);
                PathWeights.Add(int.MaxValue);
            }
        }