public static Graph_AdjacencyList_Lite ReverseGraph(Graph_AdjacencyList_Lite graphToReverse) { Graph_AdjacencyList_Lite reversedGraph = new Graph_AdjacencyList_Lite(graphToReverse.Order); foreach (int v in graphToReverse.Vertices.Keys) { reversedGraph.AddVertex(v); List<int> neighbors = graphToReverse.Vertices[v]; foreach (int neighbor in neighbors) { if (!reversedGraph.Vertices.ContainsKey(neighbor)) { reversedGraph.AddVertex(neighbor); } //reversedGraph.AddDirectedEdge(neighbor, v, graphToReverse.Edges[new Tuple<int,int>(v, neighbor)]); } } foreach (Tuple<int, int> e in graphToReverse.Edges.Keys) { reversedGraph.AddDirectedEdges(e.Item2, e.Item1, graphToReverse.Edges[e]); } return reversedGraph; }
public static void FindSCCs(Graph_AdjacencyList_Lite graph) { StronglyConnectedComponents_Kosarajus kDFS = new StronglyConnectedComponents_Kosarajus(); Dictionary<int, int> result = kDFS.FindSCCsInGraph(graph); List<int> fiveTop = FindMaxValuesInDictionary(result, 5); Console.WriteLine(string.Format("There were {0} SCCs in the graph. The leaders were {1}, 5 largest SCCs were {2}", result.Count, Utilities.FormatPrintDictionary<int, int>(result), Utilities.FormatPrintList<int>(fiveTop))); }
public static Graph_AdjacencyList_Lite ReverseGraph(Graph_AdjacencyList_Lite graphToReverse) { Graph_AdjacencyList_Lite reversedGraph = new Graph_AdjacencyList_Lite(graphToReverse.Order); foreach (int v in graphToReverse.Vertices.Keys) { reversedGraph.AddVertex(v); List<int> neighbors = graphToReverse.Vertices[v]; foreach (int neighbor in neighbors) { if (!reversedGraph.Vertices.ContainsKey(neighbor)) { reversedGraph.AddVertex(neighbor); } reversedGraph.AddDirectedEdge(neighbor, v); } } return reversedGraph; }
public Graph_AdjacencyList_Lite DeepCopy() { Graph_AdjacencyList_Lite copyToReturn = new Graph_AdjacencyList_Lite(this.Order); foreach (int v in this.Vertices.Keys) { copyToReturn.Vertices.Add(v, this.Vertices[v]); copyToReturn.Visited.Add(v, this.Visited[v]); } foreach (Tuple<int, int> e in this.Edges.Keys) { copyToReturn.Edges.Add(e, this.Edges[e]); } return copyToReturn; }