コード例 #1
0
        private List <GraphElement> CreateGraph(string graf)
        {
            String[] graphByComa = graf.Split(',');
            for (int i = 0; i < graphByComa.Length; i++)
            {
                String pair        = graphByComa[i];
                var    weight      = Regex.Match(pair, @"\d+").Value;
                var    onlyLetters = Regex.Split(pair, @"\d+");

                GraphElement element = FindInList(onlyLetters[0]);
                if (element == null)
                {
                    element = new GraphElement(onlyLetters[0]);
                    element.AddConnection(onlyLetters[1], int.Parse(weight));
                    graphElements.Add(element);
                }
                else
                {
                    element.AddConnection(onlyLetters[1], int.Parse(weight));
                }
                GraphElement elementS = FindInList(onlyLetters[1]);
                if (element == null)
                {
                    element = new GraphElement(onlyLetters[1]);
                    graphElements.Add(elementS);
                }
            }
            //  graphElements.ForEach(element => { Console.WriteLine(element.ToString()); });
            return(graphElements);
        }
コード例 #2
0
        public List <GraphElement> Dijkstra()
        {
            List <GraphElement> Q = new List <GraphElement>(graphElements);
            List <GraphElement> S = new List <GraphElement>();
            List <Cost>         d = new List <Cost>();
            List <GraphElement> p = new List <GraphElement>(new GraphElement[graphElements.Count]);

            GraphElement source = graphElements.First();

            graphElements.ForEach(element => d.Add(new Cost(element, INF)));

            Cost initialCost = FindCost(d, source);

            initialCost.cost = 0;

            while (Q.Count > 0)
            {
                Cost         lowestCost   = FindLowestCost(d, Q);
                GraphElement graphElement = lowestCost.node;

                Q.Remove(graphElement);
                S.Add(graphElement);

                foreach (Pair <string, int> connection in graphElement.connections)
                {
                    GraphElement neighbour = graphElements.Find(element => element.node1 == connection.First);
                    int          weight    = connection.Second;

                    if (!Q.Contains(neighbour))
                    {
                        continue;
                    }

                    Cost neighbourCost = FindCost(d, neighbour);
                    if (neighbourCost.cost > lowestCost.cost + weight)
                    {
                        neighbourCost.cost = lowestCost.cost + weight;
                        //trzeba znalezc odpowiednik neighbour w tablicy p
                        p[graphElements.FindIndex(element => neighbour.node1 == element.node1)] = neighbour;
                    }
                }
            }
            return(p);
        }
コード例 #3
0
 private Cost FindCost(List <Cost> d, GraphElement neighbour)
 {
     return(d.Find(cost => cost.node.node1 == neighbour.node1));
 }
コード例 #4
0
 public Cost(GraphElement node, int cost)
 {
     this.node = node;
     this.cost = cost;
 }