Ejemplo n.º 1
0
        public static IDirectedGraph <int> CreateWithCapacity(int count)
        {
            var result = new DirectedGraphAdjList <int>();

            for (int i = 1; i <= count; i++)
            {
                result.AddVertex(i);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static IDirectedGraph <int> ReverseGraph(IDirectedGraph <int> graph)
        {
            var reversedGraph = new DirectedGraphAdjList <int>(saveReversedVersion: false,
                                                               vertices: graph.GetVertices().ToArray());

            foreach (int v in graph.GetVertices())
            {
                foreach (int w in graph.GetAdjacentVertices(v))
                {
                    reversedGraph.AddEdge(w, v);
                }
            }

            return(reversedGraph);
        }
Ejemplo n.º 3
0
        public static IDirectedGraphWithReversed <int> ParseFromTextDirectedGraph(StreamReader reader, bool saveReversedVersion)
        {
            var result = new DirectedGraphAdjList <int>(saveReversedVersion);

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    continue;
                }

                var items = line.Split(' ');
                var v     = int.Parse(items[0]);
                var w     = int.Parse(items[1]);
                result.AddVertex(v);
                result.AddVertex(w);
                result.AddEdge(v, w);
            }

            return(result);
        }