Академический Документы
Профессиональный Документы
Культура Документы
Objectives
v1 0 1 0 0
v2 0 0 1 0
v3 0 0 0 0
v4 1 0 1 0
Algorithm: DFS(v)
1. Push the starting vertex, v into the stack.
2. Repeat until the stack becomes empty:
a. Pop a vertex from the stack.
b. Visit the popped vertex.
c. Push all the unvisited vertices adjacent to the popped vertex
into the stack.
v1
v1
Visited:
v1
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
v2
v4
Visited:
v1
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
v2
v4
Visited:
v1 v2
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
v6
v3
v4
Visited:
v1 v2
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
v6
v3
v4
v3
v4
Visited:
v1 v2 v6 v3
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
v5
v4
Visited:
v1 v2 v6 v3
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
v5
v4
v4
Visited:
v1 v2 v6 v3 v5 v4
Ver. 1.0 Session 17
Data Structures and Algorithms
DFS (Contd.)
Algorithm: BFS(v)
1. Visit the starting vertex, v and insert it into a queue.
2. Repeat step 3 until the queue becomes empty.
3. Delete the front vertex from the queue, visit all its unvisited
adjacent vertices, and insert them into the queue.
Visit v1
Insert v1 into the queue
v1
v1
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v1
Visited:
v1
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v2 v4
v1 v2 v4
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v2 v4
v1 v2 v4
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v4 v3 v6
v1 v2 v4 v3 v6
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v4 v3 v6 v5
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v3 v6 v5
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v6 v5
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v5
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v5
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
v1 v2 v4 v3 v6 v5
Ver. 1.0 Session 17
Data Structures and Algorithms
BFS (Contd.)
Problem Statement:
You have to represent a set of cities and the distances
between them in the form of a graph. Write a program to
represent the graph in the form of an adjacency matrix.
2
3
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 3
FINAL v1
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 3
FINAL v1 v4
5
v1 v2 = 5
v1 v4 v2 = 3 + =
3 4 6
>5
Therefore, no change is
2
3 made.
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 3
FINAL v1 v4
5
v1 v3 =
v1 v4 v3 = 3 + 2 = 5
3 4 6
5<
Therefore, the entry
2
3 corresponding to v3 in the
DISTANCE array is changed
6 3
to 5.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5
5 3
FINAL v1 v4
5
v1 v5 =
v1 v4 v5 = 3 + 6 = 9
3 4 6
9<
Therefore, the entry
2
3 corresponding to v5 in the
DISTANCE array is changed
6 3
to 9.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3
9
FINAL v1 v4
5
v1 v6 =
v1 v4 v6 = 3 + =
3 4 6
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9
FINAL v1 v4
5
From the DISTANCE array,
select the vertex with the
shortest distance from v1, such
that the selected vertex is not
3 4 6
in the FINAL array.
2
v2 and v3 have the shortest and
3 the same distance from v1.
6 3 Let us select v2 and add it to
the FINAL array.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9
FINAL v1 v4 v2
5
v1 v3 = 5
v1 v2 v3 = 5 + 4 = 9
3 4 6
9>5
Therefore, no change is
2
3 made.
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9
FINAL v1 v4 v2
5
v1 v5 = 9
v1 v2 v5 = 5 + =
3 4 6
>9
Therefore, no change is
2
3 made.
6 3
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9
FINAL v1 v4 v2
5
v1 v6 =
v1 v2 v6 = 5 + 6 = 11
3 4 6
11 <
Therefore, the entry
2
3 corresponding to v6 in the
DISTANCE array is changed
6 3
to 11.
Pass 2 complete
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9
11
FINAL v1 v4 v2
5
From the DISTANCE array,
select the vertex with the
shortest distance from v1, such
that the selected vertex is not
3 4 6
in the FINAL array.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9 11
FINAL v1 v4 v2 v3
5
v1 v5 = 9
v1 v3 v5 = 5 + 3 = 8
3 4 6
8<9
Therefore, the entry
2
3 corresponding to v5 in the
DISTANCE array is changed
6 3
to 8.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 9
8 11
FINAL v1 v4 v2 v3
5
v1 v6 = 11
v1 v3 v6 = 5 + 3 = 8
3 4 6
8 < 11
Therefore, the entry
2
3 corresponding to v6 in the
DISTANCE array is changed
6 3
to 8.
Pass 3 complete
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 11
8
FINAL v1 v4 v2 v3
5
From the DISTANCE array,
select the vertex with the
shortest distance from v1, such
that the selected vertex is not
3 4 6
in the FINAL array.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8
FINAL v1 v4 v2 v3 v5
5
v1 v6 = 8
v1 v5 v6 = 8 + =
3 4 6
>8
Therefore, no change is
2
3 made.
6 3
Pass 4 complete
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8
FINAL v1 v4 v2 v3 v5
5
Now add the only remaining
vertex, v6 to the FINAL
array.
3 4 6 All vertices have been
added to the FINAL array.
2
This means that the
3 DISTANCE array now
6 3
contains the shortest
distances from vertex v1 to
all other vertices.
v1 v2 v3 v4 v5 v6
DISTANCE 0 5 5 3 8 8
FINAL v1 v4 v2 v3 v5 v6
Problem Statement:
In the previous activity, you created a program to represent a
set of cities and the distances between them in the form of a
graph. Extend the program to include the functionality for
finding the shortest path from a given city to all the other cities.