Top Banner
Computer Science Design and Analysis of Algorithms To find out whether the graph has Hamiltonian cycle if there exists a Hamiltonian path. 2D Animation using JAVA 1
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SCS03P076Hamiltonian Path Problem (1).ppt

Computer ScienceDesign and Analysis of Algorithms

To find out whether the graph has Hamiltonian cycle if there exists a

Hamiltonian path.

2D Animation using JAVA

1

Page 2: SCS03P076Hamiltonian Path Problem (1).ppt

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

Page 3: SCS03P076Hamiltonian Path Problem (1).ppt

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

Page 4: SCS03P076Hamiltonian Path Problem (1).ppt

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

Page 5: SCS03P076Hamiltonian Path Problem (1).ppt

--GRAPH

--Red line represents HP in the Graph

5

Page 6: SCS03P076Hamiltonian Path Problem (1).ppt

Case 1: There is no HP in the Graph

6

Path 2Path 1

Page 7: SCS03P076Hamiltonian Path Problem (1).ppt

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

Page 8: SCS03P076Hamiltonian Path Problem (1).ppt

Case 2: If there exists an HP in the graph

If the Graph has HP it may have HC or not depends on the above procedure.

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 anyedge we find HP than we have HC in the original graph.But if we don’t find HP after trying with all edges than onlythe previous graph don’t have HC in it.

8

Page 9: SCS03P076Hamiltonian Path Problem (1).ppt

Example for existence of HC while there is HP

9First choosing this edge

Page 10: SCS03P076Hamiltonian Path Problem (1).ppt

Starting from this point we will try to find aHP in it.

Take any two vertices and join them with two new vertices10

Page 11: SCS03P076Hamiltonian Path Problem (1).ppt

Got stuck from this pathAnd was not able to getHP this way.

Got stuck from this pathAnd was not able to getHP this way.

Path 1

Path 2

We find out a HP in the new graph via path 2.11

Page 12: SCS03P076Hamiltonian Path Problem (1).ppt

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

12

Page 13: SCS03P076Hamiltonian Path Problem (1).ppt

Example for no existence of HC while there is HP

13

There is a HP in the graph but we’ll see that there is no HC this time.

Take any two vertices and remove the edge

Page 14: SCS03P076Hamiltonian Path Problem (1).ppt

14

Join them with two new vertices

We are going to search forHP in the new graph throughthis point.

Page 15: SCS03P076Hamiltonian Path Problem (1).ppt

Path 1

Path 2

Option 1

Option 2

15

Page 16: SCS03P076Hamiltonian Path Problem (1).ppt

Path 1

This point has now 2 options.

No HP through any further path

16

Page 17: SCS03P076Hamiltonian Path Problem (1).ppt

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 differentoption we could follow to find HP but none ofthem leads to HP

17

Page 18: SCS03P076Hamiltonian Path Problem (1).ppt

Similarly we need to check for all the edges and if we are not able to find HP through any caseThen we will conclude that the original graph doesn’t have HC

18

We removed this edge but here we wereNot able to find HP in the new graph.

Page 19: SCS03P076Hamiltonian Path Problem (1).ppt

19

Trying to find HP on removing this edge now.

Page 20: SCS03P076Hamiltonian Path Problem (1).ppt

Starting from this point

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

Page 21: SCS03P076Hamiltonian Path Problem (1).ppt

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

Page 22: SCS03P076Hamiltonian Path Problem (1).ppt

Review Questions

22

Page 23: SCS03P076Hamiltonian Path Problem (1).ppt

Saurabh Chanderiya07005004

Computer Science and Engineering.

[email protected]

CREDITS

23