Techno Blender
Digitally Yours.

Count of simple paths starting from source node

0 44


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given an undirected graph with N nodes and M edges in the form of array edg[][2], the task is to count all simple paths (paths without repeated vertices) from source node 1 in the given graph.

Examples:

Input: N = 4, edg[][2] = {{1, 2}, {2, 3}}
Output: 3
Explanation:  

  • path 1: 1
  • path 2: 1 -> 2
  • path 3: 1 -> 2 -> 3

Input: N = 4, edg[][2] = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3 4}}
Output:  16

Approach: To solve the problem follow the below idea:

The idea is to use recursion to traverse the graph starting from the source node and keep track of visited nodes and increment the counter for each path found. Traverse unvisited neighbors, and unmarks visited nodes so that multiple paths can be explored. Returns the total count of simple paths found.

Below are the steps for the above approach:

  • Declare adjacency list adj[N + 1] and iterate on all M edges and fill adjacency list.
  • Initialize a visited array of size N+1 with 0, vis[N + 1].
  • Declare a variable say ans = 0.
  • Create recursive function recur(), mark the current node visited and increment the ans by 1 and traverse its unvisited neighbors.
  • When exiting the current node, unmark the node from the visited array.
  • Return the variable ans.

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

void recur(int v, int& ans, vector<vector<int> >& adj,

           vector<int>& vis)

{

  

    

    vis[v] = 1;

  

    

    ans++;

  

    

    for (auto& u : adj[v]) {

        if (!vis[u]) {

            recur(u, ans, adj, vis);

        }

    }

  

    

    

    vis[v] = 0;

}

  

int isPossible(int N, int edg[][2], int M)

{

  

    

    vector<vector<int> > adj(N + 1);

  

    

    for (int i = 0; i < M; i++) {

        adj[edg[i][0]].push_back(edg[i][1]);

        adj[edg[i][1]].push_back(edg[i][0]);

    }

  

    

    vector<int> vis(N + 1, 0);

  

    

    int ans = 0;

  

    

    recur(1, ans, adj, vis);

  

    

    return ans;

}

  

int main()

{

  

    

    int N = 4, edg[][2] = { { 1, 2 }, { 2, 3 } };

    int M = 2;

  

    

    cout << isPossible(N, edg, M) << endl;

  

    return 0;

}

Time Complexity: O(N!)  
Auxiliary Space: O(1)


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given an undirected graph with N nodes and M edges in the form of array edg[][2], the task is to count all simple paths (paths without repeated vertices) from source node 1 in the given graph.

Examples:

Input: N = 4, edg[][2] = {{1, 2}, {2, 3}}
Output: 3
Explanation:  

  • path 1: 1
  • path 2: 1 -> 2
  • path 3: 1 -> 2 -> 3

Input: N = 4, edg[][2] = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3 4}}
Output:  16

Approach: To solve the problem follow the below idea:

The idea is to use recursion to traverse the graph starting from the source node and keep track of visited nodes and increment the counter for each path found. Traverse unvisited neighbors, and unmarks visited nodes so that multiple paths can be explored. Returns the total count of simple paths found.

Below are the steps for the above approach:

  • Declare adjacency list adj[N + 1] and iterate on all M edges and fill adjacency list.
  • Initialize a visited array of size N+1 with 0, vis[N + 1].
  • Declare a variable say ans = 0.
  • Create recursive function recur(), mark the current node visited and increment the ans by 1 and traverse its unvisited neighbors.
  • When exiting the current node, unmark the node from the visited array.
  • Return the variable ans.

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

void recur(int v, int& ans, vector<vector<int> >& adj,

           vector<int>& vis)

{

  

    

    vis[v] = 1;

  

    

    ans++;

  

    

    for (auto& u : adj[v]) {

        if (!vis[u]) {

            recur(u, ans, adj, vis);

        }

    }

  

    

    

    vis[v] = 0;

}

  

int isPossible(int N, int edg[][2], int M)

{

  

    

    vector<vector<int> > adj(N + 1);

  

    

    for (int i = 0; i < M; i++) {

        adj[edg[i][0]].push_back(edg[i][1]);

        adj[edg[i][1]].push_back(edg[i][0]);

    }

  

    

    vector<int> vis(N + 1, 0);

  

    

    int ans = 0;

  

    

    recur(1, ans, adj, vis);

  

    

    return ans;

}

  

int main()

{

  

    

    int N = 4, edg[][2] = { { 1, 2 }, { 2, 3 } };

    int M = 2;

  

    

    cout << isPossible(N, edg, M) << endl;

  

    return 0;

}

Time Complexity: O(N!)  
Auxiliary Space: O(1)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment