Week 11 Graphs
Week 11 Graphs
1
Contents
Definition
Implementation
Depth First Search
Breadth First Search
Topological sorting
Dijkstra Algorithm
Connectivity Problem
2
Graphs
3
Example
4
Adjacency
For example:
I and G are adjacent
A and C are adjacent
I and F are not adjacent
5
Path
6
Connected Graphs
7
Unconnected Graph
9
Weighted Graphs
10
Vertices: Java
Implementation
We can represent a vertex as a Java class with:
Character data
A boolean data member to check if it has been visited
11
Adjacency Matrix
12
Redundant?
14
Application: Searches
Example application
How many towns in the VN can be reached by train from
Da Lat?
Two approaches
Depth first search (DFS)
Breadth first search (BFS)
15
Depth First Search (DFS)
Idea
Pick a starting point
Follow a path to unvisited
vertices, as long as you can
until you hit a dead end
When you hit a dead end, go
back to a previous spot and hit
unvisited vertices
Stop when every path is a dead
end
16
Depth First Search (DFS)
Algorithm
Pick a vertex (call it A) as your starting point
Visit this vertex, and:
Push it onto a stack of visited vertices
Mark it as visited (so we don’t visit it again)
Visit any neighbor of A that hasn’t yet been visited
Repeat the process
When there are no more unvisited neighbors
Pop the vertex off the stack
Finished when the stack is empty
19
Depth First Search:
Complexity
Let |V| be the number of vertices in a graph
And let |E| be the number of edges
20
Breadth First Search (BFS)
Same application as DFS; we
want to find all vertices which
we can get to from a starting
point, call it A
However this time, instead of
going as far as possible until
we find a dead end, like DFS
We visit all the closest
vertices first
Then once all the closest
vertices are visited, branch
further out
21
Breadth First Search (BFS)
22
Example
23
Breadth First Search:
Complexity
Let |V| be the number of vertices in a graph
And let |E| be the number of edges
24
Minimum Spanning Trees (MSTs)
On that note of large numbers of edges slowing down
our precious search algorithms:
Let’s look at MSTs, which can help ameliorate this problem
It would be nice to take a graph and reduce the number
of edges to the minimum number required to span all
vertices:
What’s the
number of
edges
now?
25
We’ve done it already…
Actually, if you execute DFS you’ve already computed
the MST!
Think about it: you follow a path for as long as you can,
then backtrack (visit every vertex at most once)
You just have to save edges as you go
26
Directed Graphs
28
Topological Sort
Only works with DAGs (Directed Acyclic Graphs)
That is if the graph has a cycle, this will not work
31
Weighted Graphs
32
Weighted Graph: Adjacency
List
The adjacency list for a weighted graph contains edge
weights
Instead of 0 and 1
33
Weighted Graph: Adjacency List
A B C D E F
A INF INF INF INF 0.1 0.9
B 0.3 INF 0.3 0.4 INF INF
C INF INF INF 0.6 0.4 INF
D INF INF INF INF 1 INF
E 0.55 INF INF INF INF 0.45
F INF INF INF 1 INF 34
INF
Dijkstra’s Algorithm
Given a weighted graph, find the shortest path (in terms
of edge weights) between two vertices in the graph
Numerous applications
Cheapest airline fare between departure and arrival cities
Shortest driving distance in terms of mileage
35
Dijkstra’s Algorithm
B 3 C
2 5
A 2 F
1
3
1 2
D E
School of CSE – International University HCM City
1
Page 38
School of CSE – International University HCM City
Page 39
Dijkstra’s Algorithm
A C D E F
INF INF INF INF INF
40
Step 1
A C D E F
0.3 (B) 0.3 (B) 0.4 (B) INF INF
41
Step 2
A C D E F
0.3* (B) 0.3 (B) 0.4 (B) INF INF
42
Step 3
A C D E F
0.3* (B) 0.3 (B) 0.4 (B) INF INF
43
Step 4
44
Step 5
45
Step 6
Now we visit D
Which only contains
one edge to E
Now we visit E
Has two outgoing edges
One to A (marked, ignore)
One to F, which changes
the table to
0.4 + 0.45 = 0.85
0.3* (B) 0.3* (B) 0.4* (B) 0.4* (A) 0.85* (E)
Example
50
Exercise
51