// two approximation edge selector for minimum spanning tree
void twoApproximation(GraphContainer* container, Leaf** treeRoot) {

    // select the relevant nodes to the tree building algorithm
    printf("Node selector for 2-approximation.\n");

    // reset container internals
    resetContainer(container);

    // find the minimum spanning tree
    printf("Finding the minimum spanning tree.\n");
    GraphNode* root = NULL;
    minimumSpanningTree(container, &root);

    // print the root node pointer
    // printf("Root node pointer: %p\n\n", root);

    // return a bad root, if the graph root is null
    if (root == NULL) {
        (*treeRoot) = NULL;
        return;
    }

    // get a leaf and set the root
    (*treeRoot) = (Leaf*) malloc(sizeof(Leaf));
    (*treeRoot)->root = 1;
    (*treeRoot)->data = root->data;

    // build the tree
    buildTree(root, treeRoot);
}
CGraph CPathFinder::kruskalFindMST() const
{
    std::vector<CEdge> edges = getAllEdges();
    std::sort(edges.begin(), edges.end());
    CDisjointSetUnion disjointSetUnion;
    disjointSetUnion.makeFullUnion(points.size());
    CGraph minimumSpanningTree(pointsAmount);

    for (CEdge currentEdge : edges)
    {
        if (disjointSetUnion.find(currentEdge.firstVertexIndex) != disjointSetUnion.find(currentEdge.secondVertexIndex))
        {
            minimumSpanningTree.addEdgeUndirected(currentEdge);
            disjointSetUnion.unite(currentEdge.firstVertexIndex, currentEdge.secondVertexIndex);
        }
    }
    return minimumSpanningTree;
}
示例#3
0
int main()
{
    Set<nodeT *> graph;
    Map<nodeT *> graphMap;
    
	InitGraphics();
	SetWindowTitle("Pathfinder");
    
    string backgroundFile;
    bool timeToQuit = false;
    
    
    cout << "This masterful piece of work is a graph extravaganza!" << endl;
    
    while (true) {
        
        cout << "Your options are:" << endl;
        cout << "(1) Choose a new graph data file" << endl;
        cout << "(2) Find shortest path using Dijkstra's algorithm" << endl;
        cout << "(3) Compute the minimal spanning tree using Kruskal's algorithm" << endl;
        cout << "(4) Determine if the graph has cycles" << endl;
        cout << "(5) Quit" << endl;
        cout << "Enter choice: ";
        
        int choice = GetInteger();
        string datafile;
        void UpdateDisplay();
        
        switch (choice) {
            case 1:
                cout << "Enter the name of the data file: ";
                datafile = GetLine();
                //datafile = "Small.txt";
                //datafile = "USA.txt";
                //datafile = "Stanford.txt";
                graph.clear();
                graphMap.clear();
                backgroundFile = ReadGraph(graph, graphMap, datafile);
                break;
            case 2:
                if (backgroundFile == "") {
                    cout << "No file is specified" << endl;
                }
                else {
                    UpdateDisplay();
                    MovePen(0,0);
                    DrawNamedPicture(backgroundFile);
                    DrawNodesAndArcs(graph);
                    cout << "(2) Finding shortest path using Dijkstra's algorithm" << endl;
                    HandleShortestPath(graph);
                }
                break;
            case 3:
                if (backgroundFile == "") {
                    cout << "No file is specified" << endl;
                }
                else {
                    cout << "(3) Computing the minimal spanning tree using Kruskal's algorithm" << endl;
                    UpdateDisplay();
                    MovePen(0,0);
                    DrawNamedPicture(backgroundFile);
                    //DrawNodesAndArcs(graph);
                    minimumSpanningTree(graph, graphMap);
                }
                break;
            case 4:
                if (backgroundFile == "") {
                    cout << "No file is specified" << endl;
                }
                else {
                    graphT graph2;
                    graph2.allNodes = graph;
                    
                    cout << "(4) Determining if the graph has cycles" << endl;
                    if (IsCyclicGraph(graph2))
                        cout << "Yes, it is cyclic" << endl;
                    else
                        cout << "No, it is not cyclic" << endl;
                }
                break;
            case 5:
                cout << "Thanks for using Pathfinder. Bye!" << endl;
                timeToQuit = true;
                break;
                
            default:
                cout << "Invalid choice" << endl;
                break;
        }
        
        if (timeToQuit) break;
        
    }
    return (0);
}