0% found this document useful (0 votes)
21 views

Graph Traversing

Uploaded by

Rafe A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Graph Traversing

Uploaded by

Rafe A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Path

Applications: Finding a Path


 Find path from source vertex s to destination vertex d
 Use graph search starting at s and terminating as soon as we
reach d
 Need to remember edges traversed
 Use depth – first search ?
 Use breath – first search?
DFS
F B A start
DFS Process E

G D C
destination

C DFS on C D Call DFS on D


B DFS on B B B Return to call on B
A DFS on A A A A

G
Call DFS on G
D found destination - done!
B Path is implicitly stored in DFS recursion
A Path is: A, B, D, G
Breadth-First Search: The Code
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue (duh); initialize to s
while (Q not empty) {
u = RemoveTop(Q);
for each v  u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1;
v->p = u;
Enqueue(Q, v);
}
u->color = BLACK;
}
}
BFS
F B A start
BFS Process E

destination G D C

rear front rear front rear front rear front

A B D C D
Initial call to BFS on A Dequeue A Dequeue B Dequeue C
Add A to queue Add B Add C, D Nothing to add
rear front

G found destination - done!


Path must be stored separately
Dequeue D
Add G
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u  G->V
time = time+1;
{ u->d = time;
u->color = WHITE; for each v  u->Adj[]
} {
time = 0; if (v->color == WHITE)
for each vertex u  G->V DFS_Visit(v);
}
{
u->color = BLACK;
if (u->color == WHITE) time = time+1;
DFS_Visit(u); u->f = time;
} }
}
Travelling Salesman Problem
TRAVELLING SALESMAN PROBLEM
The travelling salesman problem asks the following question:
 Given a list of cities and the distances between each pair of cities, what is
the shortest possible route that visits each city exactly once and returns to
the origin city.
Hamilton Circuit
A Hamilton circuit in a graph is a circuit that visits each vertex exactly once
(returning to the starting vertex to complete the circuit).
Example: Identifying Hamilton Circuits
Refer to the graph below. Which of the following are Hamilton circuits?

a) A  B  E  D  C  F  A
b) A  B  C  D  E  F  C  B C
D
EBFA
c) B  C  D  E  F  B A
F E
Example: Identifying Hamilton Circuits
Solution
a) It is a Hamilton circuit for the graph.
b) It is not a Hamilton circuit since it visits B
more than once.
c) It is not a Hamilton circuit since it does not
visit all the vertices.
Hamilton Circuits for Complete Graphs
 Any complete graph with three of more vertices has a Hamilton circuit.

 A complete graph with n vertices has (n – 1)! Hamilton circuits.


When Hamilton Circuits Are the Same
Hamilton circuits that differ only in their starting points will be considered
to be the same circuit.
Example: Number of Circuits
How many Hamilton circuits are in a complete graph with 5 vertices?
Solution

Here n = 5, so there are (5 – 1)! = 4! = 24 Hamilton circuits.


Minimum Hamilton Circuits
In a weighted graph, a minimum Hamilton circuit is a Hamilton circuit
with the smallest possible weight.
Brute Force Algorithm
Step 1: Choose a starting point.
Step 2: List all the Hamilton circuits with that starting point.
Step 3: Find the total weight of each circuit.
Step 4: Choose a Hamilton circuit with the smallest total weight.
Example: Brute Force Algorithm
Find a minimum Hamilton circuit for the complete, weighted graph below

A 3 B
1 5
1 7

D 3 C
Example: Brute Force Algorithm
3
A B
Solution
1 1 5 7

Weight of circuit
D 3 C
1. A BCD A 14

2. A BD CA 12 Min.

14
3. A C BD A
4. A C D BA 12 (opposite of 2)
5. A D BC A 14 (opposite of 3)
6. A D CBA 14 (opposite of 1)
Nearest Neighbor Algorithm
 Step 1: Choose a starting point. Call this vertex A.
 Step 2: Check all the edges joined to A, and choose one that has the
smallest weight. Proceed along this edge to the next vertex.
 Step 3: At each vertex you reach, check the edges from there to vertices not
yet visited. Choose the smallest weight. Proceed along this edge to the next
vertex.
 Step 4: Repeat Step 3 until you have visited all the vertices.
 Step 5: Return to the starting vertex.
Example: Nearest Neighbor
The time it takes to travel between points (in minutes) is shown on the
graph below. Use the nearest neighbor algorithm to approximate the least
time to visit each location.

A
9 20
13 9 3 E
B
8 8
10 5

C 7 D
Example: Nearest Neighbor Algorithm
Solution
Start Circuit Weight
A A D E BC A 40
B BE D C A B 37
C C D E BA C 37
D D E BA C D 37
E E BD C A E 51
Example: Nearest Neighbor Algorithm
 From the information on the previous slide we would say that a route of
37 minutes should be about the least amount of time.

 Note that
A BE C D A
has a total weight of 36 minutes. All we can expect from the nearest neighbor
algorithm is a reasonably good solution.

You might also like