public static HashSet<Edge> FindMinPath(List<Edge> edges) { Graph myGraph = new Graph(); foreach (var edge in edges) { myGraph.AddNode(edge.StartNode); myGraph.AddNode(edge.EndNode); } edges.Sort(); foreach (var edge in edges) { myGraph.AddEdge(edge); } HashSet<Edge> uniqEdges = new HashSet<Edge>(); foreach (var node in myGraph.CurrentGraph) { foreach (var edge in node.Value.Conections) { uniqEdges.Add(edge); } } return uniqEdges; }
public static void Main() { Graph currentGraph = new Graph(); Node firstNode = new Node("1"); Node secondNode = new Node("2"); Node thirdNode = new Node("3"); Node fourthNode = new Node("4"); Node fifthNode = new Node("5"); Node sixthNode = new Node("6"); List<Edge> edges = new List<Edge>(); edges.Add(new Edge(firstNode, thirdNode, 5)); // 5 edges.Add(new Edge(firstNode, secondNode, 4)); edges.Add(new Edge(firstNode, fourthNode, 9)); edges.Add(new Edge(secondNode, fourthNode, 2)); edges.Add(new Edge(thirdNode, fourthNode, 20)); edges.Add(new Edge(thirdNode, fifthNode, 7)); // 7 edges.Add(new Edge(fourthNode, fifthNode, 8)); edges.Add(new Edge(fifthNode, sixthNode, 12)); // edges.Add(new Edge(firstNode, fifthNode, 4)); HashSet<Edge> result = Graph.FindMinPath(edges); foreach (var edge in result) { Console.WriteLine(edge); } }