# How to Count all possible paths between two vertices in Python

In this tutorial, we will learn how to count all possible paths between two vertices in Python with some cool and easy examples. In many situations, you might have to come up with this type of requirement.

I know you are here just because you are in need of this awesome trick to count all possible paths between two vertices in Python by using the backtracking method.

If you don’t know how to use the backtracking method then you are at the right place. Because in this tutorial we gonna find out how to use the backtracking method in Python.
Study this for backtracking:
How to implement Depth First Search algorithm in Python

## count all possible paths

Let’s learn this with some easy examples,
At first, write a function for adding edge:

```def addEdge(self, a, b):

Now let’s write the code:

```class Code:
def __init__(self, Vertices):
self.Vertices = Vertices
self.adjacent = [[] for i in range(Vertices)]
def countWays(self, x, y):
visited = [False] * self.Vertices
count = 
self.countWaysUtil(x,y, visited, count)
return count
def countWaysUtil(self, a, y,
visited, count):
visited[a] = True
if (a == y):
count += 1
else:
i = 0
visited, count)
i += 1
visited[a] = False
if __name__ == '__main__':
g = Code(4)
```3