Struktur Data & Algoritme (
Data Structures & Algorithms)
Denny ([email protected]) Suryana Setiawan ([email protected])
Fakultas Ilmu Komputer Universitas Indonesia Semester Genap - 2004/2005
Version 2.0 - Internal Use Only
Graphs
SDA/TOPIC/V2.0/2
Objectives
Understand the definition, terminology and application of graph
Students can implement graph data structures using Java
Outline
Application
Definition
Terminology
Graph Representation
Graph Application
Networks
Maps
Shortest path
Scheduling (Project Planning)
SDA/TOPIC/V2.0/5
Definition
A graph G = (V, E) is composed of:
V: set of vertices/nodes (simpul)
E: set of edges (sisi/busur)connecting the vertices in V
An edge e = (a, b) is a pair of vertices, where a and b
∈V
SDA/TOPIC/V2.0/6
Terminology
undirected graph
directed graph
adjacent vertices: connected by an edge
degree (of a vertex): # of adjacent vertices
for directed graph
• in-degree
• out-degree
SDA/TOPIC/V2.0/7
Weighted Graph
weighted graph
each edge have weighted/cost
V0 V1
V2 V3 V4
V5 V6
2 3
10
6 2 2
4
5
1
8 4
1
V = {V0, V1, V2, V3, V4, V5, V6}
SDA/TOPIC/V2.0/8
|V| = 7; |E| = 12
( ) ( ) ( ) ( )
( ) ( ) ( ) ( )
( ) ( ) ( ) ( ) ⎪ ⎭
⎪ ⎬
⎫
⎪ ⎩
⎪ ⎨
⎧
=
1 , , , 6 , , , 5 , , , 4 , ,
2 , , , 8 , , , 4 , , , 2 , ,
10 , , , 3 , , , 1 , , , 2 , ,
5 6 6 4 5 2 0 2
2 3 5 3 6 3 4 3
4 1 3 1 3 0 1 0
V V V V V V V V
V V V V V V V V
V V V V V V V V E
Weighted Graph
SDA/TOPIC/V2.0/9
path: sequence of vertices v1, v2,. . .vksuch that consecutive vertices viand vi+1are adjacent.
simple path: no repeated vertices
cycle: simple path, except that the last vertex is the same as the first vertex
DAG (Directed Acyclic Graph): directed graph with no cycles.
More Terminology
V0 V1
V2 V3 V4
V5 V6
2
3 10
6 2 2
4
5
1
8 4
1
SDA/TOPIC/V2.0/10
More Terminology
connected graph: any two vertices are connected by some path
subgraph: subset of vertices and edges forming a graph
Representation Representation: Edge List
The edge list structure simply stores the vertices and the edges into unsorted sequences.
Easy to implement.
Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence
SDA/TOPIC/V2.0/13
Representation: Adjancency List (traditional)
adjacency list of a vertex v:
sequence of vertices adjacent to v
represent the graph by the adjacency lists of all the vertices
SDA/TOPIC/V2.0/14
Representation: Adjancency List (modern)
The adjacency list structure extends the edge list structure by adding incidence containers to each vertex.
SDA/TOPIC/V2.0/15 SDA/TOPIC/V2.0/16
Representation: Adjancency Matrix (traditional)
matrix M with entries for all pairs of vertices
M[i,j] = true means that there is an edge (i,j) in the graph.
M[i,j] = false means that there is no edge (i,j) in the graph.
There is an entry for every possible edge, therefore:
Space = (N2)
SDA/TOPIC/V2.0/17
Representation: Adjancency Matrix (modern)
SDA/TOPIC/V2.0/18
Representation: Adjancency Matrix (modern)
The adjacency matrix structures augments the edge list structure with a matrix where each row and column corresponds to a vertex.
The space requirement is O(n2+ m)
Topological Sorting
A topological sort of a directed acyclic graph is an ordering on the vertices such that all edges go from left to right.
Only an acyclic graph can have a topological sort, because a directed cycle must eventually return home to the source of the cycle.
However, every DAG has at least one topological sort, and we can use depth-first search to find such an ordering.
Topological sorting proves very useful in scheduling jobs in their proper sequence
Prerequisite of subjects in your Syllabus Book! Î precedence graph
V0 V1
V2 V3 V4
V5 V6
0
1 1
2
2 3
3
Topological Sorting
Start from vertex that have in-degree 0
SDA/TOPIC/V2.0/21
V0 V1
V2 V3 V4
V5 V6
0
0 1
2
2 2
2
Topological Sorting
SDA/TOPIC/V2.0/22
V0 V1
V2 V3 V4
V5 V6
0
0 0
2
2 1
2
Topological Sorting
SDA/TOPIC/V2.0/23
V0 V1
V2 V3 V4
V5 V6
0
0 0
1
2 0
2
Topological Sorting
SDA/TOPIC/V2.0/24
V0 V1
V2 V3 V4
V5 V6
0
0 0
0
1 0
1
Topological Sorting
SDA/TOPIC/V2.0/25
V0 V1
V2 V3 V4
V5 V6
0
0 0
0
0 0
1
Topological Sorting
SDA/TOPIC/V2.0/26
V0 V1
V2 V3 V4
V5 V6
0
0 0
0
0 0
0
Topological Sorting
V0 V1
V2 V3 V4
V5 V6
0
0 0
0
0 0
0 V5
Topological Sorting
V0 V1
V2 V3 V4
V5 V6
4
2
2
2 1
1
5 6
10
8 4
3
Topological Sorting
SDA/TOPIC/V2.0/29
V0 V1
V2 V3 V4
V5 V6
4
2
2 2
1
1
5 6
10
8 4
3
Topological Sorting
SDA/TOPIC/V2.0/30
Shortest Path: unweighted graph
V0 V1
V2 V3 V4
V5 V6
V2
V6
Starting vertex: V2
Use BFS (Breath First Search) instead of DFS (Depth First Search) to find shortest path in unweighted graph (or each edge have the same weight)
0
1
1
2
2
3
3
SDA/TOPIC/V2.0/31
Shortest Path: positive weighted graph
In many applications, e.g., transportation networks, the edges of a graph have different weights.
Dijkstra’s algorithm finds shortest paths from a start vertex s to all the other vertices in a graph with
non-negative edge weights
Dijkstra’s algorithm uses a greedy method
SDA/TOPIC/V2.0/32
Dijkstra’s Algorithm
the algorithm computes for each vertex v the distance of v from the start vertex s, that is, the weight of a shortest path between s and v.
the algorithm keeps track of the set of vertices for which the shortest path has been computed (the white cloud W), the distance has been computed (the grey cloud G), and the distance has not been
computed (the black cloud B)
the algorithm uses a label D[v] to store an approximation of the distance between s and v
when a vertex v is added to the cloud, its label D[v] is equal to the actual distance between s and v
initially, the cloud W contains s, and we set
D[s] = 0
D[v] = ∞for v ≠s
SDA/TOPIC/V2.0/33
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
∞ ∞
∞
∞ ∞
∞ 0
D[V2] = 0, others ∞
Add V2into the white cloud (note: in this slide, the white cloud is the green cloud)
SDA/TOPIC/V2.0/34
Expanding the White Cloud
meaning of D[z]: length of shortest path from s to z that uses only intermediate vertices in the white cloud
after a new vertex u is added to the cloud, we need to check whether u is a better routing vertex to reach z
let u be a vertex not in the white cloud that has smallest label D[u]
we add u to the white cloud W
we update the labels of the adjacent vertices of u as follows
for each vertex z adjacent to u do if z is not in the cloud W then
if D[u] + weight(u,z) < D[z] then D[z] = D[u] + weight(u,z)
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 ∞
2
5 ∞
∞ 0
after V2added into the white cloud, update D[Vx] which Vxis the adjacent of V2
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 ∞
2
5 ∞
∞ 0
add the vertex that have minimum total weight (V3) into the white cloud
SDA/TOPIC/V2.0/37
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 ∞
2
5 6
0 4
after V3added into the white cloud, update D[Vx] which Vxis the adjacent of V3
SDA/TOPIC/V2.0/38
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 ∞
2
5 6
0 4
add the vertex that have minimum total weight (V4) into the white cloud
SDA/TOPIC/V2.0/39
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 ∞
2
5 5
0 4
after V4added into the white cloud, update D[Vx] which Vxis the adjacent of V4
SDA/TOPIC/V2.0/40
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 ∞
2
5 5
0 4
add the vertex that have minimum total weight (V0) into the white cloud
SDA/TOPIC/V2.0/41
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 7
2
5 5
0 4
after V0added into the white cloud, update D[Vx] which Vxis the adjacent of V0
SDA/TOPIC/V2.0/42
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 7
2
5 5
0 4
add the vertex that have minimum total weight (V5) into the white cloud
after V5added into the white cloud, update D[Vx] which Vxis the adjacent of V5
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 7
2
5 5
0 4
add the vertex that have minimum total weight (V6) into the white cloud
after V6added into the white cloud, update D[Vx] which Vxis the adjacent of V6
Dijkstra’s Algorithm: stages
V0 V1
V2 V3 V4
V5 V6
5
2 1 2
5 8
1 4
2
3 10
1
5 7
2
5 5
0 4
add the vertex that have minimum total weight (V1) into the white cloud
after V1added into the white cloud, update D[Vx] which Vxis the adjacent of V1
SDA/TOPIC/V2.0/45
V0 V1
V2 V3 V4
V5 V6
2 3
-10
6 2 2
4
5
1
8 4
1
Other shortest path problem
Negative-weighted Shortest-path
All-Pair Shortest Path: Floyd
SDA/TOPIC/V2.0/46
Minimum Spanning Tree (MST)
A tree formed from graph edges that connects all the vertices at lowest total cost.
The cost of a spanning tree is the sum of the costs of the edges in the tree
Application:
find the least amount of wire necessary to connect a group of homes or cities
connect all the computers in a building with the least amount of cable
SDA/TOPIC/V2.0/47
Minimum Spanning Tree (MST)
SDA/TOPIC/V2.0/48
V1 2 V2
V3 V4
V6 V7
3
10
6 7 2
4
5
1
8 4
1
V5
Minimum Spanning Tree: a graph
SDA/TOPIC/V2.0/49
V1 2 V2
V3 V4
V6
6 2
1 4 1
V5
V7
Minimum Spanning Tree
SDA/TOPIC/V2.0/50
Prim’s Algorithm
starts from one vertex
grows the rest of the tree one edge at a time.
repeatedly selects the smallest weight edge that will enlarge the tree
greedy algorithms:
we make the decision of what to do next by selecting the best local option from all available choices without regard to the global structure.
V known dV pV
V1 0 0 0
V2 0 ∞ 0
V3 0 ∞ 0
V4 0 ∞ 0
V5 0 ∞ 0
V 0 ∞ 0
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
V known dV pV
V1 1 0 0
V2 0 2 V1
V3 0 4 V1
V4 0 1 V1
V5 0 ∞ 0
V6 0 ∞ 0
V 0 ∞ 0
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
SDA/TOPIC/V2.0/53
V known dV pV
V1 1 0 0
V2 0 2 V1
V3 0 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 8 V4
V7 0 4 V4
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
SDA/TOPIC/V2.0/54
V known dV pV
V1 1 0 0
V2 1 2 V1
V3 0 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 8 V4
V7 0 4 V4
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
SDA/TOPIC/V2.0/55
V known dV pV
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 5 V3
V7 0 4 V4
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
SDA/TOPIC/V2.0/56
V known dV pV
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 0 1 V7
V7 1 4 V4
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
SDA/TOPIC/V2.0/57
V known dV pV
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 1 1 V7
V7 1 4 V4
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
SDA/TOPIC/V2.0/58
V known dV pV
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 1 6 V7
V6 1 1 V7
V7 1 4 V4
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2
Prim’s Algorithm
V known dV pV
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 1 6 V7
V6 1 1 V7
V 1 4 V
V1 2
V3 V4
V6 V7 6
2 1
4 1
V5 V2
Prim’s Algorithm Kruskal’s Algorithm
add the edges one at a time, by increasing weight
accept an edge if it does not create a cycle
SDA/TOPIC/V2.0/61
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 - (V6, V7) 1 - (V1, V2) 2 - (V3, V4) 2 - (V2, V4) 3 - (V1, V3) 4 - (V4, V7) 4 - (V3, V6) 5 - (V5, V7) 6 -
Kruskal’s Algorithm
SDA/TOPIC/V2.0/62
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 A (V6, V7) 1 - (V1, V2) 2 - (V3, V4) 2 - (V2, V4) 3 - (V1, V3) 4 - (V4, V7) 4 - (V3, V6) 5 - (V5, V7) 6 -
Kruskal’s Algorithm
SDA/TOPIC/V2.0/63
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 A (V6, V7) 1 A (V1, V2) 2 - (V3, V4) 2 - (V2, V4) 3 - (V1, V3) 4 - (V4, V7) 4 - (V3, V6) 5 - (V5, V7) 6 -
Kruskal’s Algorithm
SDA/TOPIC/V2.0/64
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 A (V6, V7) 1 A (V1, V2) 2 A (V3, V4) 2 - (V2, V4) 3 - (V1, V3) 4 - (V4, V7) 4 - (V3, V6) 5 - (V5, V7) 6 -
Kruskal’s Algorithm
SDA/TOPIC/V2.0/65
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 A (V6, V7) 1 A (V1, V2) 2 A (V3, V4) 2 A (V2, V4) 3 - (V1, V3) 4 - (V4, V7) 4 - (V3, V6) 5 - (V5, V7) 6 -
Kruskal’s Algorithm
SDA/TOPIC/V2.0/66
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 A (V6, V7) 1 A (V1, V2) 2 A (V3, V4) 2 A (V2, V4) 3 R (V1, V3) 4 R (V4, V7) 4 A (V3, V6) 5 - (V5, V7) 6 -
Kruskal’s Algorithm
V1 2
V3 V4
V6 V7
3 10
6 7 2
4
5
1
8 4
1
V5 V2 Edge Weight Action
(V1, V4) 1 A
(V6, V7) 1 A
(V1, V2) 2 A
(V3, V4) 2 A
(V2, V4) 3 R
(V1, V3) 4 R
(V4, V7) 4 A
(V3, V6) 5 R
(V5, V7) 6 A
Kruskal’s Algorithm
V1 2
V3 V4
V6 V7 6
2 1
4 1
V5 V2 Edge Weight Action
(V1, V4) 1 A
(V6, V7) 1 A
(V1, V2) 2 A
(V3, V4) 2 A
(V2, V4) 3 R
(V1, V3) 4 R
(V4, V7) 4 A
(V3, V6) 5 R
(V5, V7) 6 A
Kruskal’s Algorithm