0% found this document useful (0 votes)
62 views

Unit 2: Algorithmic Graph Theory: Course Contents

The document provides an overview of algorithms for graph theory. It discusses representations of graphs using adjacency lists and matrices. It then covers common graph algorithms including depth-first search (DFS), breadth-first search (BFS), topological sorting, and shortest path algorithms. DFS and BFS are analyzed in detail, including pseudocode, examples, and applications such as maze routing. The document also introduces concepts in graph theory including terms like vertices, edges, paths, and representations of graphs.

Uploaded by

chilledkarthik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Unit 2: Algorithmic Graph Theory: Course Contents

The document provides an overview of algorithms for graph theory. It discusses representations of graphs using adjacency lists and matrices. It then covers common graph algorithms including depth-first search (DFS), breadth-first search (BFS), topological sorting, and shortest path algorithms. DFS and BFS are analyzed in detail, including pseudocode, examples, and applications such as maze routing. The document also introduces concepts in graph theory including terms like vertices, edges, paths, and representations of graphs.

Uploaded by

chilledkarthik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit 2: Algorithmic Graph Theory

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

NTUEE/ Intro. EDA

Algorithms

Algorithm: A well-defined procedure for transforming


some input to a desired output. Major concerns:

Correctness: Does it halt? Is it correct? Efficiency: Time complexity? Space complexity?


Worst case? Average case? (Best case?)

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

NTUEE/ Intro. EDA

Example: Traveling Salesman Problem (TSP)

Instance: A set of points (cities) P together with a

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.

Correct and efficient algorithms?

Unit 2

NTUEE/ Intro. EDA

Nearest Neighbor Tour


1. pick and visit an initial point p0; 2. P p0; 3. i 0; 4. while there are unvisited points do 5. visit pi's closet unvisited point pi+1; 6. i i + 1; 7. return to p0 from pi.

Simple to implement and very efficient, but incorrect!

Unit 2

NTUEE/ Intro. EDA

A Correct, but Inefficient Algorithm


1. d ; 2. for each of the n! permutations i of the n points 3. if (cost(i) d) then 4. d cost(i); 5. Tmin i; 6. return Tmin.

Correctness: Tries all possible orderings of the points


Guarantees to end up with the shortest possible tour. Efficiency: Tries n! possible routes!

120 routes for 5 points, 3,628,800 routes for 10 points, 20 points?

No known efficient, correct algorithm for TSP!

Unit 2

NTUEE/ Intro. EDA

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

NTUEE/ Intro. EDA

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

NTUEE/ Intro. EDA

Graph

Graph: A mathematical object representing a set of


points and interconnections between them. A graph G = (V, E) consists of a set V of vertices (nodes) and a set E of directed or undirected edges.

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

NTUEE/ Intro. EDA

Example Graphs

Any binary relation is a graph.


Network of roads and cities Circuit representation

Unit 2

NTUEE/ Intro. EDA

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

Connected vertices Connected graph Connected components

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

Directed graph: edges have directions


Strongly connected components {v1}{v2, v3, v4, v5}

Weekly connected vertices

Unit 2

NTUEE/ Intro. EDA

12

Graph Representation: Adjacency List

Adjacency list: An array Adj of |V | lists, one for each


vertex in V. For each u V, Adj[u] pointers to all the vertices adjacent to u. Advantage: O(V+E) storage, good for sparse graph. Drawback: Need to traverse list to find an edge.

Unit 2

NTUEE/ Intro. EDA

13

Graph Representations: Adjacency Matrix

Adjacency matrix: A |V| r |V| matrix A = (aij) such that

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

NTUEE/ Intro. EDA

14

Explicit Edges and Vertices

Unit 2

NTUEE/ Intro. EDA

15

Tradeoffs between Adjacency List and Matrix

Unit 2

NTUEE/ Intro. EDA

16

Depth-First Search (DFS)


DFS(G) 1. for each vertex u V[G] 2. color[u] WHITE; 3. [u] NIL; 4. time 0; 5. for each vertex u V[G] 6. if color[u] = WHITE 7. DFS-Visit(u). DFS-Visit(u) 1. color[u] GRAY; /* White vertex u has just been discovered. */ 2. d[u] time time +1; 3. for each vertex v Adj[u] /* Explore edge (u,v). */ 4. if color[v] = WHITE 5. [v] u; 6. DFS-Visit(v); 7. color[u] BLACK; /* Blacken u; it is finished. */ 8. f[u] time time+1.
Unit 2

[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).

NTUEE/ Intro. EDA

17

DFS Example [Cormen]

color[u]: white gray black. Depth-first forest: G = (V, E), E = {([v], v) E | v V, [v] NIL}

{u vxy} {wz}


NTUEE/ Intro. EDA

Unit 2

18

DFS Pseudo Code in Text

Unit 2

NTUEE/ Intro. EDA

19

DFS Application 1: Topological Sort


A topological sort of a directed acyclic graph (DAG) G = (V, E) is a
linear ordering of V s.t. (u, v) E u appears before v.
Topological-Sort(G) 1. call DFS(G) to compute finishing times f[v] for each vertex v 2. as each vertex is finished, insert it onto the front of a linked list 3. return the linked list of vertices

Time complexity: O(V+E) (adjacent list).

Unit 2

NTUEE/ Intro. EDA

20

DFS Application 2: Hightowers Maze Router

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

NTUEE/ Intro. EDA

21

Breadth-First Search (BFS) [Cormen]


BFS(G,s) 1. for each vertex u V[G]-{s} 2. color[u] WHITE; 3. d[u] ; 4. [u] NIL; 5. color[s] GRAY; 6. d[s] 0; 7. [s] NIL; 8. Q {s}; 9. while Q 10. u head[Q]; 11. for each vertex v Adj[u] 12. if color[v] = WHITE 13. color[v] GRAY; 14. d[v] d[u]+1; 15. [v] u; 16. Enqueue(Q,v); 17. Dequeue(Q); 18. color[u] BLACK}.
Unit 2

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).

NTUEE/ Intro. EDA

22

BFS Example [Cormen]

Use queue for gray vertices.


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}

{(s,w), (s,r), (w,t), (w,x), (r,v), (t,u), (x,y)}


NTUEE/ Intro. EDA

Unit 2

23

BFS Pseudo Code in Text

Unit 2

NTUEE/ Intro. EDA

24

BFS Application: Lees Maze Router


Find a path from S to T by wave propagation. Discuss mainly on single-layer routing Strength: Guarantee to find a minimum-length connection
between 2 terminals if it exists. Weakness: Time & space complexity for an M N grid: O(MN) (huge!)

Unit 2

NTUEE/ Intro. EDA

25

BFS + DFS Application: Soukups Maze Router


Depth-first (line) search is first directed toward target T until
an obstacle or T is reached. Breadth-first (Lee-type) search is used to bubble around an obstacle if an obstacle is reached. Time and space complexities: O(MN), but 10--50 times faster than Lee's algorithm. Find a path between S and T, but may not be the shortest!

Unit 2

NTUEE/ Intro. EDA

26

Shortest Paths (SP)

The Shortest Path (SP) Problem


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.

Applications: weights can be distances, times, wiring


cost, delay. etc. Special case: BFS finds shortest paths for the case when all edge weights are 1.

Unit 2

NTUEE/ Intro. EDA

27

Weighted Directed Graph


A weighted, directed graph G = (V, E) with the weight
function w: E R.

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 ) .

Warning! negative-weight edges/cycles are a problem.


Cycle <e, f, e> has weight -3 < 0 (s, g) = - . Vertices h, i, j not reachable from s (s, h) = (s, i) = (s, j) = .

Algorithms apply to the cases for negative-weight


edges/cycles??

Unit 2

NTUEE/ Intro. EDA

28

Optimal Substructure of a Shortest Path


Subpaths of shortest paths are shortest paths.

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!)

Suppose that a shortest path p from a source s to a vertex v


can be decomposed into + w(u, v). . Then, (s, v) = (s, u)

For all edges (u, v) E, (s, v) (s, u) + w(u, v).

Unit 2

NTUEE/ Intro. EDA

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

NTUEE/ Intro. EDA

30

Dijkstra's Shortest-Path Algorithm


Dijkstra(G, w, s) 1. Initialize-Single-Source(G, s); 2. S ; 3. Q V[G]; 4. while Q 5. u Extract-Minimum-Element(Q); 6. S S {u}; 7. for each vertex v Adj[u] 8. Relax(u, v, w);

Idea:

search all shortest paths


In a smart way (use dynamic-programming, see next lecture)

Then choose a shortest path

Unit 2

NTUEE/ Intro. EDA

31

Example: Dijkstra's Shortest-Path Algorithm

Find the shortest path from vertex s to vertex v

sxuv ; Weight = 5+3+1

Unit 2

NTUEE/ Intro. EDA

32

Runtime Analysis of Dijkstra's Algorithm


Dijkstra(G, w, s) 1. Initialize-Single-Source(G, s); 2. S ; 3. Q V[G]; 4. while Q 5. u Extract-Minimum-Element(Q); 6. S S {u}; 7. for each vertex v Adj[u] 8. Relax(u, v, w);

Q is implemented as a linear array: O(V2).

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

Dijkstras SP Pseudo Code in Text

Unit 2

NTUEE/ Intro. EDA

34

Minimum Spanning Tree (MST)

Given an undirected graph G = (V, E) with weights on


the edges, a minimum spanning tree (MST) of G is a subset T E such that

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

NTUEE/ Intro. EDA

35

Prim's MST Algorithm


MST-Prim(G,w,r) 1. Q V[G]; 2. for each vertex u Q 3. key[u] ; 4. key[r] 0; 5. [r] NIL; 6. while Q 7. u Extract-Minimum-Element(Q); 8. for each vertex v Adj[u] 9. if v Q and w(u,v) < key[v] 10. [v] u; 11. key[v] w(u,v) Q priority queue for vertices not in the tree, based on key[]. Key[] min weight of any edge connecting to a vertex in the tree.

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

Example: Prim's MST Algorithm

Unit 2

NTUEE/ Intro. EDA

37

Time Complexity of Prim's MST Algorithm


MST-Prim(G,w, r) 1. Q V[G]; 2. for each vertex u Q 3. key[u] ; 4. key[r] 0; 5. [r] NIL; 6. while Q 7. u Extract-Minimum-Element(Q); 8. for each vertex v Adj[u] 9. if v Q and w(u,v) < key[v] 10. v] u; 11. key[v] w(u,v)

Straightforward implementation: O(V2) time


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

Prims MST Pseudo Code in Text

Unit 2

NTUEE/ Intro. EDA

39

You might also like