Exemplo n.º 1
0
        public void Print(GraphWeighted graph)
        {
            var adjList = graph.AdjacencyList;

            foreach (var v1 in adjList)
            {
                var counter = 0;
                Console.Write(v1.Key + ": ");
                foreach (var v2 in v1.Value)
                {
                    var pair = "[" + v2.Key + ";" + v2.Value + "]";

                    if (counter < v1.Value.Count - 1)
                    {
                        Console.Write(pair + "->");
                    }
                    else
                    {
                        Console.Write(pair);
                    }
                    counter++;
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 2
0
        public GraphWeighted Kruskal()
        {
            var edges       = new List <Edge>();
            var resultGraph = new GraphWeighted();

            foreach (var v in AdjacencyList)
            {
                foreach (var e in v.Value)
                {
                    var edge = new Edge(v.Key, e.Key, e.Value);
                    if (!edges.Contains(edge))
                    {
                        edges.Add(edge);
                    }
                }
            }

            edges.Sort((left, right) => left.Weight - right.Weight);
            edges.ForEach(edge => resultGraph.AddNoCycle(edge));

            return(resultGraph);
        }