23
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

SCS03P076Hamiltonian Path Problem (1).ppt

Embed Size (px)

Citation preview

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

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

6

Path 2Path 1

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

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

Example for existence of HC while there is HP

9First choosing this edge

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

Take any two vertices and join them with two new vertices10

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

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

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

14

Join them with two new vertices

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

Path 1

Path 2

Option 1

Option 2

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

17

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.

19

Trying to find HP on removing this edge now.

Starting from this point

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

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

22

Saurabh Chanderiya07005004

Computer Science and Engineering.

[email protected]

CREDITS

23