Unit 2: Algorithmic Graph Theory: Course Contents
Unit 2: Algorithmic Graph Theory: Course Contents
Course contents:
Introduction to graph theory Basic graph algorithms Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2nd Ed., McGraw Hill/MIT Press, 2001.
Reading
Unit 2
Algorithms
Better algorithms?
How: Faster algorithms? Algorithms with less space requirement? Optimality: Prove that an algorithm is best possible/optimal? Establish a lower bound?
Unit 2
distance d(p, q) between any pair p, q P. Output: What is the shortest circular route that starts and ends at a given point and visits all the points.
Unit 2
Unit 2
Unit 2
Example: Sorting
Instance: A sequence of n numbers <a1, a2, , an>. Output: A permutation <a1', a2', , an'> such that a1'
a2' an'. Input: <8, 6, 9, 7, 5, 2, 3> Output: <2, 3, 5, 6, 7, 8, 9 > Correct and efficient algorithms?
Unit 2
Insertion Sort
InsertionSort(A) 1. for j 2 to length[A] do 2. key A[j]; 3. /* Insert A[j] into the sorted sequence A[1..j-1]. */ 4. i j - 1; 5. while i > 0 and A[i] > key do 6. A[i+1] A[i]; 7. i i - 1; 8. A[i+1] key;
Unit 2
Graph
V is the vertex set: V = {v1, v2, v3, v4, v5, v6}, |V|=6 E is the edge set: E = {e1, e2, e3, e4, e5}, |E|=5 An edge has two endpoints, e.g. e1 = (v1, v2) For simplicity, use V for |V| and E for |E|.
Unit 2
Example Graphs
Unit 2
Terminology
Degree of a vertex: degree(v3) = 3, degree(v2) = 2 Subgraph of a graph: Complete (sub)graph: V = {v1, v2, v3}, E = {e1, e2, e3} (Maximal/maximum) clique: maximal/maximum complete
subgraph Selfloop Parallel edges Simple graph Multigraph
Unit 2
NTUEE/ Intro. EDA
10
Terminology (contd)
Bipartite graph G = (V1, V2, E) Path Cycle: a closed path
A bipartite graph
Unit 2
Path p = <v1, v2, v3, v4> Cycle C = <v1, v2, v3, v1 >
NTUEE/ Intro. EDA
11
Terminology (contd)
Weighted graph:
Edge weighted and/or vertex weighted Directed path Directed cycle Directed acyclic graph (DAG) In-degree, out-degree Strongly connected vertices
Unit 2
12
Unit 2
13
Advantage: O(1) time to find an edge. Drawback: O(V2) storage,suitable for dense graph. How to save space if the graph is undirected?
Unit 2
14
Unit 2
15
Unit 2
16
[Cormen]
color[u]:
white (undiscovered) gray (discovered) black (explored: out edges are all discovered) d[u]: discovery time (gray) f[u]: finishing time (black) [u]: predecessor Time complexity: O(V+E) (adjacency list).
17
color[u]: white gray black. Depth-first forest: G = (V, E), E = {([v], v) E | v V, [v] NIL}
Unit 2
18
Unit 2
19
Unit 2
20
A single escape point on each line segment. If a line parallels to the blocked cells, the escape point
is placed just past the endpoint of the segment. Time and space complexities: O(L), where L is the # of line segments generated.
Unit 2
21
color[u]:
white (undiscovered) gray (discovered) black (explored: out edges are all discovered) d[u]: distance from source s [u]: predecessor of u Use queue for gray vertices Time complexity: O(V+E) (adjacency list).
22
Each vertex is enqueued and dequeued once: O(V) time. Each edge is considered once: O(E) time. G = (V, E), V = {v V| [v] NIL} {s}
Breadth-first tree:
E = {([v], v) E | v V - {s}}.
{s, w, r, t, x, v, u, y}
Unit 2
23
Unit 2
24
Unit 2
25
Unit 2
26
Given: A directed graph G=(V, E) with edge weights, and a specific source node s. Goal: Find a minimum weight path (or cost) from s to every other node in V.
Unit 2
27
Weight of path p = <v0, v1, , vk>: w(p) = Shortest-path weight from u to v, (u, v):
k i =1
w(v i 1, v i ) .
Cycle <e, f, e> has weight -3 < 0 (s, g) = - . Vertices h, i, j not reachable from s (s, h) = (s, i) = (s, j) = .
Unit 2
28
Let p = <v1, v2, , vk> be a shortest path from vertex v1 to vertex vk, and pij = <vi, vi+1, , vj> be the subpath of p from vertex vi to vertex vj, 1 i j k. Then, pij is a shortest path from vi to vj. (NOTE: reverse is not necessarily true!)
Unit 2
29
Relaxation
Initialize-Single-Source(G, s) 1. for each vertex v V[G] 2. d[v] ; /* upper bound on the weight of a shortest path from s to v */ 3. [v] NIL; /* predecessor of v */ 4. d[s] 0; Relax(u, v, w) 1. if d[v] > d[u]+w(u, v) 2. d[v] d[u]+w(u, v); 3. [v] u;
d[v] d[u] + w(u, v) after calling Relax(u, v, w). d[v] (s, v) during the relaxation steps; once d[v] achieves its lower
bound (s, v), it never changes. Let be a shortest path. If d[u] = (s, u) prior to the call Relax(u, v, w), then d[v] = (s, v) after the call.
Unit 2
30
Idea:
Unit 2
31
Unit 2
32
Line 5: O(V) for Extract-Minimum-Element, so O(V2) with the while loop. Lines 7--8: O(E) operations, each takes O(1) time.
Q is implemented as a binary heap: O(E lg V). Q is implemented as a Fibonacci heap: O(E + V lg V).
Unit 2
NTUEE/ Intro. EDA
33
Unit 2
34
T has no cycles T contains all vertices in V sum of the weights of all edges in T is minimum.
Number of edges in T is number of vertices minus one Applications: circuit interconnection (minimizing tree
radius), communication network (minimizing tree diameter), etc.
Unit 2
35
Starts from a vertex and grows until the tree spans all the
vertices.
The edges in A always form a single tree. At each step, a safe, minimum-weighted edge connecting a vertex in A to a vertex in V - A is added to the tree.
NTUEE/ Intro. EDA
Unit 2
36
Unit 2
37
Lines 1--5: O(V). Line 7: O(V) for Extract-Minimum-Element, so O(V2) with the while loop. Lines 8--11: O(E) operations, each takes O(lgV) time.
Run in O(E lg V) time if Q is implemented as a binary heap Run in O(E + VlgV) time if Q is implemented as a Fibonacci heap
Unit 2
NTUEE/ Intro. EDA
38
Unit 2
39