Find the paths between two given nodes?

前端 未结 8 2064
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 11:55

Say I have nodes connected in the below fashion, how do I arrive at the number of paths that exist between given points, and path details?

1,2 //node 1 and 2         


        
相关标签:
8条回答
  • 2020-11-27 12:41

    For those who are not PYTHON expert ,the same code in C++

    //@Author :Ritesh Kumar Gupta
    #include <stdio.h>
    #include <vector>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <iostream>
    using namespace std;
    vector<vector<int> >GRAPH(100);
    inline void print_path(vector<int>path)
    {
        cout<<"[ ";
        for(int i=0;i<path.size();++i)
        {
            cout<<path[i]<<" ";
        }
        cout<<"]"<<endl;
    }
    bool isadjacency_node_not_present_in_current_path(int node,vector<int>path)
    {
        for(int i=0;i<path.size();++i)
        {
            if(path[i]==node)
            return false;
        }
        return true;
    }
    int findpaths(int source ,int target ,int totalnode,int totaledge )
    {
        vector<int>path;
        path.push_back(source);
        queue<vector<int> >q;
        q.push(path);
    
        while(!q.empty())
        {
            path=q.front();
            q.pop();
    
            int last_nodeof_path=path[path.size()-1];
            if(last_nodeof_path==target)
            {
                cout<<"The Required path is:: ";
                print_path(path);
            }
            else
            {
                print_path(path);
            }
    
            for(int i=0;i<GRAPH[last_nodeof_path].size();++i)
            {
                if(isadjacency_node_not_present_in_current_path(GRAPH[last_nodeof_path][i],path))
                {
    
                    vector<int>new_path(path.begin(),path.end());
                    new_path.push_back(GRAPH[last_nodeof_path][i]);
                    q.push(new_path);
                }
            }
    
    
    
    
        }
        return 1;
    }
    int main()
    {
        //freopen("out.txt","w",stdout);
        int T,N,M,u,v,source,target;
        scanf("%d",&T);
        while(T--)
        {
            printf("Enter Total Nodes & Total Edges\n");
            scanf("%d%d",&N,&M);
            for(int i=1;i<=M;++i)
            {
                scanf("%d%d",&u,&v);
                GRAPH[u].push_back(v);
            }
            printf("(Source, target)\n");
            scanf("%d%d",&source,&target);
            findpaths(source,target,N,M);
        }
        //system("pause");
        return 0;
    }
    
    /*
    Input::
    1
    6 11
    1 2 
    1 3
    1 5
    2 1
    2 3
    2 4
    3 4
    4 3
    5 6
    5 4
    6 3
    1 4
    
    output:
    [ 1 ]
    [ 1 2 ]
    [ 1 3 ]
    [ 1 5 ]
    [ 1 2 3 ]
    The Required path is:: [ 1 2 4 ]
    The Required path is:: [ 1 3 4 ]
    [ 1 5 6 ]
    The Required path is:: [ 1 5 4 ]
    The Required path is:: [ 1 2 3 4 ]
    [ 1 2 4 3 ]
    [ 1 5 6 3 ]
    [ 1 5 4 3 ]
    The Required path is:: [ 1 5 6 3 4 ]
    
    
    */
    
    0 讨论(0)
  • 2020-11-27 12:46

    If you want all the paths, use recursion.

    Using an adjacency list, preferably, create a function f() that attempts to fill in a current list of visited vertices. Like so:

    void allPaths(vector<int> previous, int current, int destination)
    {
        previous.push_back(current);
    
        if (current == destination)
            //output all elements of previous, and return
    
        for (int i = 0; i < neighbors[current].size(); i++)
            allPaths(previous, neighbors[current][i], destination);
    }
    
    int main()
    {
        //...input
        allPaths(vector<int>(), start, end);
    }
    

    Due to the fact that the vector is passed by value (and thus any changes made further down in the recursive procedure aren't permanent), all possible combinations are enumerated.

    You can gain a bit of efficiency by passing the previous vector by reference (and thus not needing to copy the vector over and over again) but you'll have to make sure that things get popped_back() manually.

    One more thing: if the graph has cycles, this won't work. (I assume in this case you'll want to find all simple paths, then) Before adding something into the previous vector, first check if it's already in there.

    If you want all shortest paths, use Konrad's suggestion with this algorithm.

    0 讨论(0)
提交回复
热议问题