Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Stopwatch stopwatch = new Stopwatch();

            Graph graph = new Graph(10);

            graph.AddDoubleEdge(0, 1);
            graph.AddDoubleEdge(2, 1);
            graph.AddDoubleEdge(3, 1);
            graph.AddDoubleEdge(0, 4);
            graph.AddDoubleEdge(3, 2);
            graph.AddDoubleEdge(3, 6);
            graph.AddDoubleEdge(3, 5);
            graph.AddDoubleEdge(4, 7);
            graph.AddDoubleEdge(5, 8);
            graph.AddDoubleEdge(5, 9);

            BFS.PrintBFS(graph, 1);
            stopwatch.Start();
            Console.WriteLine(BFS.ContainsBFS(graph, 1, 6));
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
            Console.WriteLine(BFS.ContainsBFS(graph, 1, 10));

            DFS.PrintDFS(graph.nodes[1]);
            stopwatch.Reset();
            stopwatch.Start();
            Console.WriteLine(DFS.ContainsDFS(graph.nodes[1], 6));
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
            Console.WriteLine(DFS.ContainsDFS(graph.nodes[1], 10));

            Console.ReadKey();
        }
Exemplo n.º 2
0
        private static void DoDFS(int v, int[][] vs)
        {
            Console.WriteLine();

            var graph = new Graph(v, vs);
            var dfs   = DFS.Traverse(graph, 0);

            foreach (var t in dfs)
            {
                Console.Write($"{t} ");
            }
        }
Exemplo n.º 3
0
        static public void Main()
        {
            List <int> val = ReadInputAndParseList();
            int        v   = val[0];
            int        e   = val[1];

            List <int>[] adj = new List <int> [v];
            for (int i = 0; i < v; i++)
            {
                adj[i] = new List <int>();
            }
            for (int i = 0; i < e; i++)
            {
                List <int> arr = ReadInputAndParseList();
                int        x   = arr[0];
                int        y   = arr[1];

                adj[x].Add(y);
                //adj[y].Add(x);
            }

            for (int i = 0; i < v; i++)
            {
                Console.Write($"{i}");
                foreach (var item in adj[i])
                {
                    Console.Write($"-> {item}");
                }
                Console.WriteLine();
            }
            BFS        bfs    = new BFS();
            List <int> bfsOut = bfs.BFSTraversal(adj, v);

            foreach (int item in bfsOut)
            {
                Console.Write($"{item}, ");
            }

            DFS        dfs    = new DFS();
            List <int> dfsOut = dfs.DFSTraversal(adj, v);

            foreach (int item in dfsOut)
            {
                Console.Write($"{item}, ");
            }
        }
Exemplo n.º 4
0
        static void Main()
        {
            //initialize graph
            var graph = new List <int>[]
            {
                new List <int> {
                    3, 6
                },                  //zero is connected with 3 and 6
                new List <int> {
                    2, 3, 4, 5, 6
                },                        //first is connected with 2,3,4..
                new List <int> {
                    1, 4, 5
                },
                new List <int> {
                    0, 1, 5
                },
                new List <int> {
                    1, 2, 6
                },
                new List <int> {
                    1, 2, 3
                },
                new List <int> {
                    0, 1, 4
                }                    //sixth is connected with the rest
            };

            DFS dfs = new DFS(graph);
            BFS bfs = new BFS(graph);

            for (int i = 0; i < graph.Length; i++)
            {
                dfs.Dfs(i);
            }

            Console.WriteLine();

            for (int i = 0; i < graph.Length; i++)
            {
                bfs.Bfs(i);
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            IGraph <int> myGraf;

            myGraf = new MatrixGraph <int>(true);
            //myGraf = new ListGraph<int>(false);
            myGraf.AddVertex(new Vertex <int>(0, 3));
            myGraf.AddVertex(new Vertex <int>(1, 2));
            myGraf.AddVertex(new Vertex <int>(2, 3));
            myGraf.AddVertex(new Vertex <int>(3, 2));
            myGraf.AddVertex(new Vertex <int>(4, 3));
            myGraf.AddVertex(new Vertex <int>(5, 3));
            myGraf.AddVertex(new Vertex <int>(6, 3));



            myGraf.AddEdge(new Edge <int>(myGraf.getVertexById(0), myGraf.getVertexById(4)));

            myGraf.AddEdge(new Edge <int>(myGraf.getVertexById(1), myGraf.getVertexById(3)));

            Edge <int> myEdge = new Edge <int>(myGraf.getVertexById(0), myGraf.getVertexById(1));

            //Console.WriteLine(myGraf.ToString());


            //Console.Write("\nTEST USUWANIE KRAWEDZI \n");
            //myGraf.AddEdge(myEdge);
            //Console.WriteLine(myGraf.ToString());
            //myGraf.RemoveEdge(myEdge);
            //Console.WriteLine(myGraf.ToString());


            //myGraf.AddEdge(myEdge);
            //Console.Write("\nTEST SASIEDZI WIERZCHOLKA 0  \n");
            //List<Vertex<int>> zeroVertexNeighbours = myGraf.getVertexById(0).Neighbors;
            //foreach (var vertex in zeroVertexNeighbours)
            //{
            //    Console.Write(vertex.Id + ", ");
            //}



            DFS <int> dfs = new DFS <int>(myGraf);

            List <Vertex <int> > Dfsed = dfs.ExecuteDFS(myGraf.getVertexById(0));

            foreach (var item in Dfsed)
            {
                Console.Write(item.Id + ", ");
            }



            Dictionary <Edge <int>, AvalibleEdgeTypes> edgeClassification = dfs.ClasifyEdges();

            foreach (var edge in edgeClassification)
            {
                Console.Write(edge.Key.FromVertex.Id + " - " + edge.Key.ToVertex.Id + " : " + edge.Value.ToString() + "\n");
            }
            int n = Console.Read();
        }