private static void Main(string[] args)
        {
            var graph = new Graph <char>();

            graph.AddNode('A');
            graph.AddNode('B');
            graph.AddNode('C');
            graph.AddNode('D');
            graph.AddNode('E');
            graph.AddNode('F');
            graph.AddEdge('A', 'B');
            graph.AddEdge('A', 'C');
            graph.AddEdge('A', 'D');
            graph.AddEdge('B', 'C');
            graph.AddEdge('B', 'E');
            graph.AddEdge('C', 'D');
            graph.AddEdge('C', 'E');
            graph.AddEdge('C', 'F');
            graph.AddEdge('D', 'F');
            var visited = new List <char>();

            graph.BreadthFirstTraverse('A', ref visited);
            for (var i = 0; i < visited.Count; i++)
            {
                Console.WriteLine(visited[i]);
            }
            Console.ReadKey();
        }
Example #2
0
        private Graph <string> GetGraphData()
        {
            Graph <string> web = new Graph <string>();

            web.AddNode("Privacy.htm");
            web.AddNode("People.aspx");
            web.AddNode("About.htm");
            web.AddNode("Index.htm");
            web.AddNode("Products.aspx");
            web.AddNode("Contact.aspx");

            web.AddDirectedEdge("People.aspx", "Privacy.htm");   // People -> Privacy

            web.AddDirectedEdge("Privacy.htm", "Index.htm");     // Privacy -> Index
            web.AddDirectedEdge("Privacy.htm", "About.htm");     // Privacy -> About

            web.AddDirectedEdge("About.htm", "Privacy.htm");     // About -> Privacy
            web.AddDirectedEdge("About.htm", "People.aspx");     // About -> People
            web.AddDirectedEdge("About.htm", "Contact.aspx");    // About -> Contact

            web.AddDirectedEdge("Index.htm", "About.htm");       // Index -> About
            web.AddDirectedEdge("Index.htm", "Contact.aspx");    // Index -> Contacts
            web.AddDirectedEdge("Index.htm", "Products.aspx");   // Index -> Products

            web.AddDirectedEdge("Products.aspx", "Index.htm");   // Products -> Index
            web.AddDirectedEdge("Products.aspx", "People.aspx"); // Products -> People

            return(web);
        }
Example #3
0
        static void Main(string[] args)
        {
            var graph = new Graph();

            graph.AddNode("X");
            graph.AddNode("A");
            graph.AddNode("B");
            graph.AddNode("P");

            graph.AddEdge("X", "A");
            graph.AddEdge("X", "B");
            graph.AddEdge("A", "P");
            graph.AddEdge("B", "P");
            graph.AddEdge("P", "X");

            //graph.Print();

            //Console.WriteLine("Removing node...");
            //graph.RemoveNode("A");
            //graph.RemoveEdge("B", "C");

            //graph.Print();

            //graph.TraverseBreadthFirstIterative("X");

            var dep = new List <string>();

            dep = graph.TopologicalSort();

            foreach (var node in dep)
            {
                Console.WriteLine(node);
            }

            Console.WriteLine();
            Console.WriteLine("Graph contains loop: " + graph.HasCycle());
            Console.WriteLine();
        }
Example #4
0
        static void Main(string[] args)
        {
            GraphNode <char> current;                      //current location
            GraphNode <char> to;                           //current location

            GraphNode <char> current1;                     //current location

            GraphNode <char> to1;                          //current location
            Graph <char>     myGraph = new Graph <char>(); //current location

            myGraph.AddNode('A');
            myGraph.AddNode('B');

            myGraph.AddNode('X');
            myGraph.AddNode('Y');

            myGraph.AddEdge('A', 'B');

            myGraph.AddEdge('X', 'Y');
            myGraph.AddEdge('Y', 'X');



            current = myGraph.GetNodeByID('X');
            to      = myGraph.GetNodeByID('Y');

            current1 = myGraph.GetNodeByID('A');
            to1      = myGraph.GetNodeByID('B');

            Console.WriteLine("Is my graph empty? Answer : {0}", myGraph.IsEmptyGraph());
            Console.WriteLine("Does myGraph contain {0}? Answer: {1}", current.ID, myGraph.ContainsGraph(current));
            Console.WriteLine("Is node {0} and {1} adjacent? Answer: {2}", to.ID, current.ID, myGraph.IsAdjacent(to, current));
            Console.WriteLine("Is node {0} and {1} adjacent? Answer: {2}", current.ID, to.ID, myGraph.IsAdjacent(current, to));
            Console.WriteLine("Is node {0} and {1} adjacent? Answer: {2}", to1.ID, current1.ID, myGraph.IsAdjacent(to1, current1));
            Console.WriteLine("Is node {0} and {1} adjacent? Answer: {2}", current1.ID, to1.ID, myGraph.IsAdjacent(current1, to1));
            Console.ReadLine();
        }
Example #5
0
        private static void Main(string[] args)
        {
            // elements of a graph to generate
            int    numberOfElements    = 15;
            int    lowerBoundOfWeights = 0;
            int    upperBoundOfWeights = 30;
            Random rnd = new Random();
            // use letters starting with 'A' as a vertices names
            List <string>  letters = Enumerable.Range(65, numberOfElements).Select(elm => ((char)elm).ToString()).ToList();
            Graph <string> graph   = new Graph <string>();

            letters.ForEach(letter => graph.AddNode(letter));
            // get all vertices of a newly created graph
            List <Vertex <string> > vertices = graph.GetNodes();

            // generate 10 random edges
            for (int i = 0; i < numberOfElements; i++)
            {
                // take two random vertices
                List <Vertex <string> > pair = vertices.OrderByDescending(_ => rnd.Next(0, 10)).Take(2).ToList();
                // add edge with a random weight
                graph.AddEdge(pair[0], pair[1], rnd.Next(lowerBoundOfWeights, upperBoundOfWeights));
            }

            // output the graph to the console
            Console.Write("Adjacency list respresentation of a random graph");
            foreach (var item in graph)
            {
                Console.WriteLine();
                Console.Write($"{item.Value}: ");
                graph.GetNeighbors(item)
                .ToList()
                .ForEach(elm => Console.Write($"[{elm.Key}, {elm.Value}], "));
            }
            Console.ReadLine();
        }
Example #6
0
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (radioButtonNode.Checked)
            {
                Graphics gr = CreateGraphics();
                graph.AddNode(e.X, e.Y);
                int len = graph.nodes.Length;

                graph.nodes[len - 1].DrawNode(gr);
                gr.Dispose();
            }
            else
            {
                if (radioButtonEdge.Checked)
                {
                    int n = graph.FindNode(e.X, e.Y);
                    if (n != -1)
                    {
                        DrawEdge = true;
                        nodeIn   = n;
                    }
                }
            }
        } //MouseDown
Example #7
0
        static void Main()
        {
            /*-------------------Undirected and unweighted edges------------------------------*/

            //Graph<int> graph = new Graph<int>(false, false); // initiate a unidirectional graph

            //Node<int> n1 = graph.AddNode(1); // add numbered nodes
            //Node<int> n2 = graph.AddNode(2);
            //Node<int> n3 = graph.AddNode(3);
            //Node<int> n4 = graph.AddNode(4);
            //Node<int> n5 = graph.AddNode(5);
            //Node<int> n6 = graph.AddNode(6);
            //Node<int> n7 = graph.AddNode(7);
            //Node<int> n8 = graph.AddNode(8);

            //graph.AddEdge(n1, n2); // add edges between nodes
            //graph.AddEdge(n1, n3);
            //graph.AddEdge(n2, n4);
            //graph.AddEdge(n3, n4);
            //graph.AddEdge(n4, n5);
            //graph.AddEdge(n5, n6);
            //graph.AddEdge(n5, n7);
            //graph.AddEdge(n5, n8);
            //graph.AddEdge(n6, n7);
            //graph.AddEdge(n7, n8);

            //List<Node<int>> dfsNodes = graph.DFS(); // returns a list of Node instances
            //dfsNodes.ForEach(n => WriteLine(n));

            /*-------------------Undirected and weighted edges------------------------------*/

            //Graph<int> graph = new Graph<int>(false, true);

            //Node<int> n1 = graph.AddNode(1); // add numbered nodes
            //Node<int> n2 = graph.AddNode(2);
            //Node<int> n3 = graph.AddNode(3);
            //Node<int> n4 = graph.AddNode(4);
            //Node<int> n5 = graph.AddNode(5);
            //Node<int> n6 = graph.AddNode(6);
            //Node<int> n7 = graph.AddNode(7);
            //Node<int> n8 = graph.AddNode(8);

            // graph.AddEdge(n1, n2, 3);
            // graph.AddEdge(n1, n3, 5);
            // graph.AddEdge(n2, n4, 4);
            // graph.AddEdge(n3, n4, 12);
            // graph.AddEdge(n4, n5, 9);
            // graph.AddEdge(n4, n8, 8);
            // graph.AddEdge(n5, n6, 4);
            // graph.AddEdge(n5, n7, 5);
            // graph.AddEdge(n5, n8, 1);
            // graph.AddEdge(n6, n7, 6);
            // graph.AddEdge(n7, n8, 20);
            // graph.AddEdge(n2, n6, 20);
            // graph.AddEdge(n2, n5, 20);

            /*-------------------Directed and weighted edges------------------------------*/
            WriteLine("New Graph 1");
            Graph <int> graph = new Graph <int>(true, true);

            Node <int> n1 = graph.AddNode(1); // you can add these to a file
            Node <int> n2 = graph.AddNode(2);
            Node <int> n3 = graph.AddNode(3);
            Node <int> n4 = graph.AddNode(4);
            Node <int> n5 = graph.AddNode(5);
            Node <int> n6 = graph.AddNode(6);
            Node <int> n7 = graph.AddNode(7);
            Node <int> n8 = graph.AddNode(8);

            graph.AddEdge(n1, n2, 9); // you can add these to a file
            graph.AddEdge(n1, n3, 5);
            graph.AddEdge(n2, n1, 3);
            graph.AddEdge(n2, n4, 18);
            graph.AddEdge(n3, n4, 12);
            graph.AddEdge(n4, n2, 2);
            graph.AddEdge(n4, n8, 8);
            graph.AddEdge(n5, n4, 9);
            graph.AddEdge(n5, n6, 2);
            graph.AddEdge(n5, n7, 5);
            graph.AddEdge(n5, n8, 3);
            graph.AddEdge(n6, n7, 1);
            graph.AddEdge(n7, n5, 4);
            graph.AddEdge(n7, n8, 6);
            graph.AddEdge(n8, n5, 3);

            List <Node <int> > dfsNodes = graph.DFS(); // returns a list of Node instances

            dfsNodes.ForEach(n => WriteLine(n));



            /*-------------------Kruskal's Algorithm------------------------------*/

            //List<Edge<int>> mstKruskal = graph.MSTKruskal();
            //mstKruskal.ForEach(e => WriteLine(e));



            /*-------------------Prim's Algorithm------------------------------*/

            //List<Edge<int>> mstPrim = graph.MSTPrim();
            //mstPrim.ForEach(e => WriteLine(e));

            /*-------------------Depth-First Search------------------------------*/

            //List<Node<int>> dfsNodes = graph.DFS(); // returns a list of Node instances
            //dfsNodes.ForEach(n => WriteLine(n));

            /*-------------------Breadth-first Search------------------------------*/
            //List<Node<int>> bfsNodes = graph.BFS(); // returns a list of Node instances
            //bfsNodes.ForEach(n => WriteLine(n));



            /*-------------------telecomm cable------------------------------*/
            //WriteLine("Start example here: \n");

            //Graph<string> graph = new Graph<string>(false, true);

            //Node<string> nodeb1 = graph.AddNode("b1");
            //Node<string> nodeb2 = graph.AddNode("b2");
            //Node<string> nodeb3 = graph.AddNode("b3");
            //Node<string> nodeb4 = graph.AddNode("b4");
            //Node<string> nodeb5 = graph.AddNode("b5");
            //Node<string> nodeb6 = graph.AddNode("b6");
            //Node<string> noder1 = graph.AddNode("r1");
            //Node<string> noder2 = graph.AddNode("r2");
            //Node<string> noder3 = graph.AddNode("r3");
            //Node<string> noder4 = graph.AddNode("r4");
            //Node<string> noder5 = graph.AddNode("r5");
            //Node<string> noder6 = graph.AddNode("r6");

            //graph.AddEdge(nodeb1, nodeb2, 2);
            //graph.AddEdge(nodeb1, nodeb3, 20);
            //graph.AddEdge(nodeb1, nodeb4, 30);
            //graph.AddEdge(nodeb2, nodeb4, 20);
            //graph.AddEdge(nodeb2, nodeb3, 30);
            //graph.AddEdge(nodeb3, nodeb4, 2);
            //graph.AddEdge(nodeb2, noder2, 25);
            //graph.AddEdge(nodeb4, noder4, 25);
            //graph.AddEdge(noder1, noder2, 1);
            //graph.AddEdge(noder2, noder3, 1);
            //graph.AddEdge(noder3, noder4, 1);
            //graph.AddEdge(noder1, noder5, 75);
            //graph.AddEdge(noder3, noder6, 100);
            //graph.AddEdge(noder5, noder6, 3);
            //graph.AddEdge(noder6, nodeb6, 10);
            //graph.AddEdge(noder6, nodeb5, 3);
            //graph.AddEdge(nodeb5, nodeb6, 6);


            //List<Edge<string>> mstPrim = graph.MSTPrim();
            //mstPrim.ForEach(e => WriteLine(e));
            //WriteLine("Total Cost: " + mstPrim.Sum(e => e.Weight));
        }
Example #8
0
        static void Main(string[] args)
        {
            var graph = new Graph();

            //graph.AddNode("a");
            //graph.AddNode("b");
            //graph.AddNode("c");
            //graph.AddNode("d");
            //graph.AddNode("e");
            //graph.AddNode("f");
            //graph.AddNode("g");

            //graph.AddEdge("a", "b", 5);
            //graph.AddEdge("b", "c", 9);
            //graph.AddEdge("b", "e", 4);
            //graph.AddEdge("b", "f", 1);
            //graph.AddEdge("c", "d", 3);
            //graph.AddEdge("c", "f", 2);
            //graph.AddEdge("d", "f", 7);
            //graph.AddEdge("d", "e", 5);
            //graph.AddEdge("e", "f", 6);
            //graph.AddEdge("e", "g", 7);


            graph.AddNode("Hermannplatz");
            graph.AddNode("Alexanderplatz");
            graph.AddNode("Hauptbahnhof");
            graph.AddNode("Zoo");
            graph.AddNode("Wittenau");
            graph.AddNode("Rudow");
            graph.AddNode("Kochstraße");
            graph.AddNode("KottbuserTor");
            graph.AddNode("Wedding");
            graph.AddNode("Steglitz");
            graph.AddNode("Pankow");
            graph.AddNode("Tempelhpf");

            graph.AddEdge("Hermannplatz", "Zoo", 5);
            graph.AddEdge("Wittenau", "Alexanderplatz", 8);
            graph.AddEdge("Hermannplatz", "Alexanderplatz", 10);
            graph.AddEdge("Zoo", "Wittenau", 11);
            graph.AddEdge("Wedding", "Zoo", 4);
            graph.AddEdge("Wedding", "Tempelhpf", 3);
            graph.AddEdge("Wedding", "KottbuserTor", 7);
            graph.AddEdge("Wedding", "Steglitz", 7);
            graph.AddEdge("Steglitz", "Rudow", 4);
            graph.AddEdge("Tempelhpf", "Pankow", 8);
            graph.AddEdge("Wittenau", "Kochstraße", 2);
            //graph.AddEdge("Wittenau", "Tempelhpf", 4);
            graph.AddEdge("Kochstraße", "Hauptbahnhof", 15);


            // graph.DisplayAllNodes();

            // graph.NodesNextTo("Tempelhpf");


            var start   = graph.Nodes.First(n => n.value == "Hermannplatz");
            var ziel    = graph.Nodes.First(n => n.value == "Wittenau");
            var history = new List <Node>();
            var s       = graph.SearchWaysRecursive(start, ziel, history);

            var count = 0;

            foreach (var solution in s)
            {
                var ar = solution.ToArray();
                if (ar[0] == start)
                {
                    count++;
                    Console.WriteLine(" Weg :" + count);
                    foreach (var step in solution)
                    {
                        Console.WriteLine(step.value + "  ");
                    }
                    var cost = graph.WaysCosts(solution);
                    Console.WriteLine("Costs are :" + cost);
                }
            }
            //var history = new System.Collections.Generic.List<Node>
            //{
            //    start
            //};

            // graph.DFSListWay("Hermannplatz","Zoo");



            Console.ReadLine();



            //if(!graph.RemoveNode("Zoo" ))// removeEdges: true))
            //{
            //    Console.WriteLine("Löschen ging nicht");
            //}



            //Console.WriteLine("--------------  NACH REMOVE ------------------");
            //graph.DisplayAllNodes();


            //var hermann = graph.Nodes.Find(t => t.Name == "Wedding");

            //Console.WriteLine(hermann.Edges.Exists(e => e.Start.Name == "Zoo"));
        }
Example #9
0
        static void Main(string[] args)
        {
            GraphNode <int> current;
            GraphNode <int> to;
            Graph <int>     myGraph = new Graph <int>();



            myGraph.AddNode(0);
            myGraph.AddNode(1);
            myGraph.AddNode(2);
            myGraph.AddNode(3);
            myGraph.AddNode(4);
            myGraph.AddNode(5);
            myGraph.AddNode(6);

            myGraph.AddEdge(0, 1);
            myGraph.AddEdge(0, 2);
            myGraph.AddEdge(1, 3);
            myGraph.AddEdge(4, 1);
            myGraph.AddEdge(5, 2);
            myGraph.AddEdge(5, 6);
            myGraph.AddEdge(6, 0);
            myGraph.AddEdge(6, 4);


            current = myGraph.GetNodeByID(0);
            to      = myGraph.GetNodeByID(2);

            //to.GetAdjList.Contains('A');

            Console.WriteLine("Is myGraph empty? Answer: {0}", myGraph.IsEmptyGraph());
            Console.WriteLine("Is myGraph contain {0} ? Answer: {1}", current.ID, myGraph.ContainsGraph(current));
            Console.WriteLine("Is node {0} and {1} adjacent ? Answer : {2}", current.ID, to.ID, myGraph.IsAdjacent(current, to));
            Console.WriteLine("Is node {0} and {1} adjacent ? Answer : {2}", to.ID, current.ID, myGraph.IsAdjacent(to, current));
            List <int> test3 = new List <int>();

            myGraph.DepthFirstTraverse(5, ref test3);
            foreach (int c in test3)
            {
                System.Console.WriteLine("Reacheable items item " + c);
            }



            foreach (int c in myGraph.mothervertex())
            {
                Console.WriteLine("vertex " + c);
            }
            // System.Console.WriteLine(myGraph.mothervertex());

            List <int> test1 = new List <int>();

            test1.Add(1);
            test1.Add(2);
            test1.Add(3);


            List <int> test2 = new List <int>();

            test2.Add(1);
            test2.Add(2);
            test2.Add(3);
            test2.Add(4);

            System.Console.WriteLine("Same lists {0} ", myGraph.ListEq(test1, test2));

            System.Console.WriteLine("---------------------- Nodes BEFORE topological sort -------------");
            System.Console.WriteLine("graph time " + myGraph.TIME);
            foreach (GraphNode <int> node in myGraph.Nodes)
            {
                System.Console.WriteLine(" node id " + node.ID);
                System.Console.WriteLine(" node discovery time" + node.DISCOVERY);
                System.Console.WriteLine(" node finishing time" + node.FINISHING);
                System.Console.WriteLine(" node color" + node.COLOR);
                System.Console.WriteLine("\n");
            }
            List <GraphNode <int> > orderdList = new List <GraphNode <int> >();

            //orderdList.AddFirst(new GraphNode<int>(4));
            //orderdList.AddFirst(new GraphNode<int>(5));
            //orderdList.AddFirst(new GraphNode<int>(6));


            //foreach (GraphNode<int> node in orderdList)
            //{
            //    System.Console.WriteLine(" -------node id " + node.ID);
            //    System.Console.WriteLine(" --------node discovery time" + node.DISCOVERY);
            //    System.Console.WriteLine(" --------node finishing time" + node.FINISHING);
            //    System.Console.WriteLine(" --------node color" + node.COLOR);
            //    System.Console.WriteLine("\n");
            //}


            orderdList = myGraph.TopologicalSort();
            System.Console.WriteLine("graph time " + myGraph.TIME);

            System.Console.WriteLine("---------------------- Nodes after topological sort -------------");
            foreach (GraphNode <int> node in orderdList)
            {
                System.Console.WriteLine(" node id " + node.ID);
                System.Console.WriteLine(" adjacent node count " + node.GetAdjList.Count);
                System.Console.WriteLine(" node discovery time" + node.DISCOVERY);
                System.Console.WriteLine(" node finishing time" + node.FINISHING);
                System.Console.WriteLine(" node color" + node.COLOR);
                System.Console.WriteLine("\n");
                // break;
            }

            Console.ReadKey();
        }