Lecture 9 - Graph Part 2
Lecture 9 - Graph Part 2
STRUCTURES
LECTURE 9 – GRAPHS (PART II)
Shyryn Tutkyshbayeva
[email protected]
CONTENT
1. Adjacency List Review
2. Search
3. Depth-first search
4. Breadth-first search
5. Edge-weighted graphs
6. The shortest path
7. Dijkstra’s algorithm
ADJACENCY LIST (REVIEW)
1 2 3 4
adj[]
5 6 AdjList[0] = (1, 2, 3, 4)0 0 5 6
AdjList[1] = (0, 5, 6) 1
0 7
AdjList[2] = (0, 7) 2
1 AdjList[3] = (0) 3 0
AdjList[4] = (0) 4 0
AdjList[5] = (1) 5
4 0 2 7
6 1
AdjList[6] = (1)
AdjList[7] = (2) 7 1
3
2
ADJACENCY LIST (REVIEW)
adj[]
0 nullnull
1 nullnull
2 nullnull
3 null
null
4 null
5 null
null
6 nullnull
7 nullnull
SEARCH
Why do we need to search $50 PVD
$70 ORD
graphs? SFO
$2
$130
00
1. Path problems: e.g. What is 70
$80
$ 1 LGA
$60
the shortest path from $250 $140
node A to node B? HNL $120 00
LAX $110 $ 1
DFW
2. Connectivity problems: e.g, $500 MIA
If we can reach from node
A to node B? What is the shortest path from MIA to SFO?
3. Spanning tree problems: Which path has the minimum cost?
e.g. Find the minimal
spanning tree
SEARCH
There are two standard graph traversal techniques:
1. Depth-First Search (DFS)
2. Breadth-First Search (BFS)
In both DFS and BFS, the nodes of the undirected graph are
visited in a systematic manner so that every node is visited
exactly one.
DEPTH-FIRST SEARCH
5 6 1
1 2 3
4 0 2 7
4 5 6 7
3
DEPTH-FIRST SEARCH
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit
it, and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent
the new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.
A stack data structure is used to support
backtracking when implementing the DFS
DEPTH-FIRST SEARCH
1 2 3 4
adj[]
0 0 5 6
1
0 7
2
3 0
4 0
5
6 1
7 1
2
BREADTH-FIRST SEARCH
5 6 1
1 2 3
4 0 2 7
4 5 6 7
3
BREADTH-FIRST SEARCH
BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the
root in a BFS tree being formed. Its level is called
the current level.
2. From each node z in the current level, in the order
in which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes
from this level form a new level that becomes the
next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step
1.
A queue data structure is used when
implementing the BFS
BREADTH-FIRST SEARCH
1 2 3 4
adj[]
0 0 5 6
1
0 7
2
3 0
4 0
5
6 1
7 1
2
EDGE-WEIGHTED GRAPHS
An edge-weighted graph is a graph model
where we associate weights or costs with
each edge
Example Applications: Route for Yandex
taxi where the weight might represent
Distance
Approximate time
Average speed
Or all the above for that section of road
Visited
vertex
B C D E F
Change the
Choose the shortest
distancespath,
to other
which
vertices,
is to vertex
if there
B, is
and visit
found a shorter
it path
DIJKSTRA’S ALGORITHM
When the algorithm finds the shortest path between two nodes, that node
is tagged as "visited" and added to the path
The method is repeated until the path contains all the nodes in the graph
Only graphs with positive weights can be used by Dijkstra's Algorithm.
This is because the weights of the edges must be added
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
LITERATURE
Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne, Addison-
Wesley
Chapter 4