Ejemplo n.º 1
0
        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 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;
        }