-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathnumberofpaths.cpp
59 lines (51 loc) · 1011 Bytes
/
numberofpaths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
class Graph {
int V;
list<int>* adj;
void countPathsUtil(int, int, int&);
public:
Graph(int V);
void addEdge(int u, int v);
int countPaths(int s, int d);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
}
int Graph::countPaths(int s, int d)
{
int pathCount = 0;
countPathsUtil(s, d, pathCount);
return pathCount;
}
void Graph::countPathsUtil(int u, int d,
int& pathCount)
{
if (u == d)
pathCount++;
else {
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
countPathsUtil(*i, d, pathCount);
}
}
int main()
{
Graph g(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.addEdge(1, 4);
g.addEdge(2, 3);
int s = 0, d = 4;
cout << g.countPaths(s, d);
return 0;
}