 Hello everyone, I am Vinit Thribune from WIT Solapur and today we shall see detection of Hamiltonian cycle in a graph by using backtracking approach. So at the end of this session you will be able to apply backtracking approach to find all Hamiltonian cycle in a given graph. So what exactly is this particular problem? We are given a graph which comprises of set V and E where V is the set of vertices, E is the set of edges and we have n vertices in this graph. A Hamiltonian cycle is a round trip path along all the n vertices where we are covering all the n vertices of the graph and we visit every vertex or every node exactly once. We cannot visit any of the vertices more than once. So this is the problem, in other words if the cycle begins at vertex V1 and the vertices are visited in the order V1, V2, V3 which are adjacent, then the edge that is the last edge I mean the Vi and the Vi plus 1 belongs to edge set E and Vi is equal to Vn plus 1. What that means is the cycle or the Hamiltonian tour will end at where the cycle started. So the first and the last node will be exactly same. So this is the sample graph it has five nodes and connected edges. So the possible Hamiltonian cycles in if we consider this graph is say for example if we start from 1 we will tour from 1 to 2, 2 to 3, 3 to 4, 4 to 5 and back from 5 to 1. So this is a possible Hamiltonian cycle in this graph. Similarly there is another cycle where we tour from 1 to 2, 2 to 4, 4 to 3, 3 to 5 and back from 5 to 1. So these are two of the possible Hamiltonian cycle if we consider the sample graph. If we consider this particular graph if you say for example we start from 1 we will tour from 1 to 2, 2 to 4 and if you go to any of the vertices that is either 5 or 6 you will not be able to cover the other vertices apart from coming back to 4. So this will not satisfy the constraint that this problem has given and 4 will be visited twice. So this is there is no Hamiltonian cycle in this graph and since it does not satisfy all the constraints. So how to apply backtracking approach to find the solution to the Hamiltonian cycle problem? We generate a n cross n adjacency matrix marking 1 for possible edge between two vertices and 0 if there is no possible edge. We start seeking for the cycle by setting the first vertex at the starting vertex because we want to set the starting point of the cycle. We continue checking by visiting adjacency vertices exactly once. If we reach at the last vertex we check whether edge from that last visited vertex to the first vertex is available if the edge is available the cycle is found. If all the vertices are visited and no cycle is found say for example no cycle is found we backtrack to the previous vertex to continue checking for other paths leading to the cycle. If there is no cycle that is the last vertex does not lead to the first vertex of the cycle we backtrack and then check for the cycles from the remaining adjacent vertices. All the paths that return back to the starting vertex by visiting every node exactly once are counted as Hamiltonian cycle. So we have to follow these three criteria we follow every vertex should be visited exactly once. There should be an edge between the current vertex and the previous visited vertex and the last vertex should have an edge joining to the first vertex to form a valid Hamiltonian cycle. So these these are the criteria that we follow to find the solution. Now let's go ahead and find the solution to this graph. So we have already seen this graph now I will show you how to apply backtracking approach to find a solution to this particular graph. Now as we have discussed I have generated an adjacency matrix where one states that there is a edge from the node say for example 1 to 4 if there is a edge I have marked it as 1 because 1 to 4 has a valid edge 1 to 3 does not have a valid edge. So I have marked it as 0 and likewise for all other pair of nodes. So we start our solution initially we mark all the the array or the vertices that we have visited as 0 because we have not yet started our solution. So first we fix the starting vertex of the cycle to 1 that is node 1 to avoid finding the same cycles again and again. So we start the we fix the starting vertex to 1. Now we start traversing the vertices from 1 to 5 to find the cycle. Now as we have discussed we cannot visit one single vertex twice in a cycle. So we have to move ahead we cannot stick to 1 or we cannot visit 1 again. So we have to go ahead okay so we can go from 1 to 2. Now again we start from 1 so we cannot visit 1 again. Similarly we cannot visit 2 again. So the next vertex that we can visit is 3 because 2 to 3 is a valid edge. Now again we start from 1, 1 cannot be visited again similarly 2 cannot be visited again. 3 also cannot be visited since it has been visited previously. So the next vertex that we can visit from 3 is 4 because it has a valid edge. So we write 4 here. Now the next vertex to find is we start from 1, 1 cannot be visited since it has been visited previously. 2 also has been visited previously. Similarly 3 and 4 also cannot be visited since both of them have been visited previously. So we come to 5 and 4 to 5 can be visited since there is a valid edge from 4 to 5. Now once all vertices have been visited exactly once we check whether there is an edge from the last vertex to the first vertex. In our case the last vertex is 5, the first vertex is 1. So we see whether an edge from the last vertex to the first vertex is available and yes since 5 to 1 is available or is 1 we have come to our first solution and the Hamiltonian cycle in this case is 1 to 2, 2 to 3, 3 to 4, 4 to 5 and back from 5 to 1. So this is a valid solution that we have found. We are going to find some more solutions to this same graph. So what we do is since we cannot go ahead that I cannot write 6 because there are only 5 nodes we backtrack from the last visited vertex and since no other vertex are available we bound the path by marking 0 and backtrack from the previous vertex. So I write 0 and now I will backtrack 4 to 4 to see for further available solutions for the same graph. So the next vertex from 3 to 4 is 3 to 5. So I will write 5 here since there is a valid vertex from 3 to 5. Now I will start again finding solutions from 1 here. So I cannot visit 1 because 1 is already been visited. I also cannot visit 2 again because 2 again has been previously visited in our solution. 3 also cannot be visited since it has been visited previously. Now if you see I can visit 4 but I have to check whether there is an edge from 5 to 4. Now it shows me that there is an edge from 5 to 4. So visiting 4 after 5 is completely possible and so the next vertex that I visit from 5 to 4 from after 5 is 4. I cannot visit 1, 2, 3 because they have been previously visited, 4 can be visited but the thing to check is whether there is an edge from 5 to 4. So there is an edge from 5 to 4. Now we have to see whether there is an edge from 4 to 1. So we see from the graph as well as from the matrix that 4 to 1 is available. So this is our second solution that we have found out that the cycle starts from 1 to 2 to 3 to 5, it goes to 4 and back from 4 to 1. So this is again a valid solution that we have found out. So again we continue further and check for other cycles. So we cannot visit 5 again since it has been previously visited, we backtrack to 0. Now again we cannot go ahead from 5 to 0, so from 5 to 6 so we make it as 0 and we backtrack. Now I can visit from 2 to 4. So 2 to 4 is a valid edge, so we can visit 4 after 2. Now I will continue further and check for other vertices for cycles starting from 1. One cannot be visited since it has been previously visited, similarly 2 cannot be visited again. We have to check for 3. Now 4 to 3 is a valid edge, so visiting 3 after 4 is completely possible and now we will check for other vertices after 3 so that we can complete the cycle and reach up till 1. So we start from 1, 1 cannot be visited, it has been previously visited. Similarly 2 and 3 also cannot be visited, 4 also has been previously visited. So we leave 4 also and now we see whether there is an edge from 3 to 5. So since there is an edge from 3 to 5, so we traverse from 3 to 5 and check whether 5 connects us back to the starting vertex 1. If 5 is connecting us to 1 since we have seen in the previous solution 5 is giving us a valid solution 5 to 1, so we come to our third solution that is cycle from 1 to 2 to 4, 4 to 3, 3 to 5 and back from 5 to 1. So these three solutions of course there are many other solutions also but we will stop here and we have come to three solutions by using backtracking approach. So at this point in time I want you to pause the video, draw the particular graph and find or identify whether the graph has a Hamiltonian cycle or not. Pause the video and answer the question. So if you carefully see the graph and try to trace the solution, the solution graph has a Hamiltonian cycle and it is as shown in the slide. So this is the reference that I have used for the video. Thank you.