Example #1
0
        static void Debug2(WGraph tree2)
        {
            Console.Write("The list of nodes is: \n");
            Console.Write(tree2.ListNodes() + "\n\n");

            Console.Write("The graph edgelist is: \n");
            Console.Write(tree2.DisplayEdges() + "\n\n");

            Console.Write("The graph edge matrix is\n");
            Console.Write(tree2.DisplayMatrix() + "\n");
        }
Example #2
0
    public static Dictionary <NodeType, NodeType> FindPath <NodeType>(WGraph <NodeType> graph, NodeType startNode)
    {
        Dictionary <NodeType, int>      weights    = new Dictionary <NodeType, int>(); //node weights
        Dictionary <NodeType, bool>     hasVisited = new Dictionary <NodeType, bool>();
        Dictionary <NodeType, NodeType> path       = new Dictionary <NodeType, NodeType>();

        Queue <NodeType> q = new Queue <NodeType>();

        q.Enqueue(startNode);
        weights.Add(startNode, 0);
        while (q.Count != 0)
        {
            NodeType n = q.Peek();
            if (!hasVisited.ContainsKey(n))
            {
                hasVisited.Add(n, true);
                q.Dequeue();

                foreach (var neighbor in graph.Neighbors(n))
                {
                    if (weights.ContainsKey(neighbor))
                    {
                        if (weights[neighbor] > graph.getW(neighbor) + weights[n])
                        {
                            weights[neighbor] = graph.getW(neighbor) + weights[n];
                            path[neighbor]    = n;
                        }
                    }
                    else
                    {
                        weights[neighbor] = graph.getW(neighbor) + weights[n];
                        path[neighbor]    = n;
                    }
                    if (!hasVisited.ContainsKey(neighbor))
                    {
                        q.Enqueue(neighbor);
                    }
                }
            }
            else
            {
                q.Dequeue();
            }
        }
        return(path);
    }
Example #3
0
    public static List <NodeType> GetPath <NodeType>(WGraph <NodeType> graph, NodeType startNode, NodeType endNode)
    {
        List <NodeType> path = new List <NodeType>();
        Dictionary <NodeType, NodeType> full_path = FindPath <NodeType>(graph, startNode);

        if (full_path.ContainsKey(endNode))
        {
            NodeType td = endNode;
            path.Insert(0, td); //

            while (!path[0].Equals(startNode))
            {
                td = full_path[td];
                path.Insert(0, td);
            }
        }
        return(path);
    }
Example #4
0
        static void MinCostTest()
        {
            Console.Write("This test displays a minimum cost spanning tree for a weighted graph\n\n");

            // create the graph
            WGraph tree2 = new WGraph();

            // add nodes
            tree2.AddNode('A');
            tree2.AddNode('C');
            tree2.AddNode('T');
            tree2.AddNode('Z');
            tree2.AddNode('X');
            tree2.AddNode('K');
            tree2.AddNode('Q');
            tree2.AddNode('J');
            tree2.AddNode('M');
            tree2.AddNode('U');

            // add edges
            tree2.AddWEdge('A', 'C', 3);
            tree2.AddWEdge('A', 'T', 4);
            tree2.AddWEdge('A', 'Z', 2);
            tree2.AddWEdge('X', 'C', 4);
            tree2.AddWEdge('C', 'K', 8);
            tree2.AddWEdge('T', 'Q', 4);
            tree2.AddWEdge('K', 'Q', 3);
            tree2.AddWEdge('Q', 'J', 6);
            tree2.AddWEdge('J', 'M', 5);
            tree2.AddWEdge('Z', 'X', 6);

            // uncomment the next line to see your node list, edge list, and edge matrix
            Debug2(tree2);

            Console.Write("The min cost tree should be: \n");
            Console.Write("Q: Q-K Q-T T-A A-Z A-C C-X Q-J J-M \n");

            Console.Write("The min cost tree is: \n");
            Console.Write(tree2.MinCostTree('Q') + "\n");

            Console.Write("Done with testing min cost spanning tree \n\n");
        }