1 / 23

Computer Science Design and Analysis of Algorithms

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.

uyen
Download Presentation

Computer Science Design and Analysis of Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. --GRAPH --Red line represents HP in the Graph 5

  6. Case 1: There is no HP in the Graph Path 2 Path 1 6

  7. 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

  8. 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.

  9. Example for existence of HC while there is HP First choosing this edge 9

  10. 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

  11. 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.

  12. Since we are able to find a HP in the new graph therefore there exist a HC in the original graph 12

  13. 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

  14. Join them with two new vertices We are going to search for HP in the new graph through this point. 14

  15. Option 1 Option 2 Path 2 Path 1 15

  16. Path 1 This point has now 2 options. No HP through any further path 16

  17. 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

  18. 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

  19. Trying to find HP on removing this edge now. 19

  20. Starting from this point 20 Similarly as previous example after trying every possible path we didn’t reach HP.

  21. 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

  22. 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

  23. CREDITS SaurabhChanderiya 07005004 Computer Science and Engineering. saurabhc@cse.iitb.ac.in 23

More Related