230 likes | 397 Views
Computer Science Design and Analysis of Algorithms. 2D Animation using JAVA. To find out whether the graph has Hamiltonian cycle if there exists a Hamiltonian path. 1. Hamiltonian Path (HP). A Hamiltonian path is a path in an undirected graph which visits each vertex exactly once.
E N D
Computer ScienceDesign and Analysis of Algorithms 2D Animation using JAVA To find out whether the graph has Hamiltonian cycle if there exists a Hamiltonian path. 1
Hamiltonian Path (HP) A Hamiltonian path is a path in an undirected graph which visits each vertex exactly once. Hamiltonian cycle (HC) A Hamiltonian cycle (or Hamiltonian circuit) is a cycle in an undirected graph which visits each vertex exactly once and also returns to the starting vertex. 2
Aim and procedure • Aim is to show this algorithm by animation so that it become easy to visualize what is going on with removing edges and vertices. • Procedure: • If it doesn’t contains HP: If it doesn’t have Hamiltonian Path then it will definitely won’t have HC. As every HC has HP in it. • If it contains HP : In this case graph may have HC or may not. For that we need to remove edge and new vertices in the graph. 3
Problem Statement Input: • A graph • Yes(If graph has HP) • No(If graph doesn’t have HP) Output: • Yes(If the graph has HC) • No(If the graph doesn’t have HC) 4
--GRAPH --Red line represents HP in the Graph 5
Case 1: There is no HP in the Graph Path 2 Path 1 6
We can see that if the graph doesn’t have HP in it then it cannot have HC in it in any case. HP is a subset-path of HC and if there is no HP than their cannot be HC . We saw this by the above example. 7
Case 2: If there exists an HP in the graph Procedure : Remove one edge and join the two vertices with two newly created vertices. Now check whether that new graph has HP or not. If it has HP than the original graph has HC in it. Note : We have to do this for all the edges if altering any edge we find HP than we have HC in the original graph. But if we don’t find HP after trying with all edges than only the previous graph don’t have HC in it. 8 If the Graph has HP it may have HC or not depends on the above procedure.
Example for existence of HC while there is HP First choosing this edge 9
Starting from this point we will try to find a HP in it. 10 Take any two vertices and join them with two new vertices
Path 1 Got stuck from this path And was not able to get HP this way. Path 2 11 We find out a HP in the new graph via path 2.
Since we are able to find a HP in the new graph therefore there exist a HC in the original graph 12
Example for no existence of HC while there is HP There is a HP in the graph but we’ll see that there is no HC this time. 13 Take any two vertices and remove the edge
Join them with two new vertices We are going to search for HP in the new graph through this point. 14
Option 1 Option 2 Path 2 Path 1 15
Path 1 This point has now 2 options. No HP through any further path 16
Path 2 Similarly here we have 3 options and similarly doing the above steps to find HP we didn’t reach HP. Blue green and Red lines show the different option we could follow to find HP but none of them leads to HP 17
We removed this edge but here we were Not able to find HP in the new graph. Similarly we need to check for all the edges and if we are not able to find HP through any case Then we will conclude that the original graph doesn’t have HC 18
Trying to find HP on removing this edge now. 19
Starting from this point 20 Similarly as previous example after trying every possible path we didn’t reach HP.
We tried for the above two edges, similarly trying for every edge we were not able to find HP Conclusion: After checking with all the edges, we find there doesn’t exists HP therefore the Original graph doesn’t contain HC in it. 21
Review Questions 1. If there is HP in a graph than does it necessarily have HC in it. (Answer – No) 2. If there is no HP in a graph than does it necessarily don’t have HC in it. (Answer – Yes) 3. Do HC covers all vertices. ( Answer – Yes) 4. Do black lines in the shown graph represents HC.(Answer – No, because in making of that • path you need to pass the centre vertex • twice, which is not allowed in HC) 22
CREDITS SaurabhChanderiya 07005004 Computer Science and Engineering. saurabhc@cse.iitb.ac.in 23