public static void ex4() { WeightedGraphOOP <string, int> graph = new WeightedGraphOOP <string, int>(); graph.AddNode("A"); graph.AddNode("B"); graph.AddNode("C"); graph.AddNode("D"); graph.AddNode("E"); graph.AddEdge("A", "B", 3); graph.AddEdge("A", "C", 5); //graph.AddEdge("B", "C", 1); graph.AddEdge("C", "D", 1); graph.AddEdge("D", "E", 2); graph.AddEdge("E", "C", 7); Console.WriteLine(graph.HasCycle()); var spanningTree = graph.MakeSpanningTree(); spanningTree.print(); }
public static void ex2() { WeightedGraphOOP <string, int> graph = new WeightedGraphOOP <string, int>(); graph.AddNode("A"); graph.AddNode("B"); graph.AddNode("C"); graph.AddEdge("A", "B", 3); graph.AddEdge("A", "C", 5); graph.print(); }
public static void ex3() { WeightedGraphOOP <string, int> graph = new WeightedGraphOOP <string, int>(); graph.AddNode("A"); graph.AddNode("B"); graph.AddNode("C"); graph.AddEdge("A", "B", 3); graph.AddEdge("A", "C", 5); graph.AddEdge("B", "C", 1); Console.WriteLine(graph.GetShortestDistance("A", "C")); List <string> list = (List <string>)graph.GetShortestPath("A", "C"); foreach (var item in list) { Console.Write(item + " "); } Console.WriteLine(); }
public WeightedGraphOOP <T1, T2> MakeSpanningTree() { WeightedGraphOOP <T1, T2> spanningTree = new WeightedGraphOOP <T1, T2>(); SimplePriorityQueue <NodePriorityQueue> queue = new SimplePriorityQueue <NodePriorityQueue>(); var startNode = Nodes.Values.FirstOrDefault(); if (startNode is null) { throw new NullReferenceException(); } queue.Enqueue(new NodePriorityQueue(startNode, null, default), 0); while (spanningTree.Nodes.Count < Nodes.Count) { var n = queue.Dequeue(); if (spanningTree.Nodes.ContainsKey(n.Node.Value)) { continue; } spanningTree.AddNode(n.Node.Value); if (!(n.Source is null)) { spanningTree.AddEdge(n.Source.Value, n.Node.Value, n.Weight); } foreach (var list in n.Node.Edges.Values) { foreach (var item in list) { dynamic priority = item.Weigth; queue.Enqueue(new NodePriorityQueue(item.To, item.From, item.Weigth), priority); } } } return(spanningTree); }