Docsity

Graph Algorithms, Lecture Slides - Computer Science, Slides of Introduction to Computers

Partial preview of the text

Download Graph Algorithms, Lecture Slides - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!

COMS21103: Introduction to Graph Algorithms

Raphaël Clifford [email protected]

Bristol University, Department of Computer Science Bristol BS8 1UB, UK

November 5, 2009

Raphaël Clifford [email protected]

Graphs

A graph is a standard data structure for modelling real-world information. In general, graphs occur whenever there are connections or dependencies between objects. I (^) Computer networks I (^) Electronic circuits, microprocessors I (^) Maps, geographic information systems I (^) Flow charts, system design diagrams I (^) Physical or bureaucratic structures I (^) Planning and scheduling, course prerequisites We will look at greedy algorithms for two of the most important problems on graphs. These are finding the minimum spanning tree and finding shortest paths in graphs.

Raphaël Clifford [email protected]

Weighted Graphs

A weighted graph has a number associated with each edge. I (^) Weights can represent distance, cost of a move, capacity of a pipe. I (^) We will assume that weights are always non-negative. I (^) A weighted graph can be directed or undirected.

Raphaël Clifford [email protected]

Distances and Weighted Graphs

The length ( or weight ) of a path is the sum of the weights of the edges on the path. I (^) The distance between two nodes is the length of the shortest path between them I (^) If no path exists, distance is set to ∞ or INF. I (^) However often convenient to set to zero, in which case need to be careful in some algorithms.

Raphaël Clifford [email protected]

Adjacency matrix example

a b

c d

Has the adjacency matrix

a b c d a 0 1 1 1 b 1 0 0 1 c 1 0 0 0 d 1 1 0 0

Raphaël Clifford [email protected]

Graph Representation - List

a b

c d

Adjacency Lists I (^) a → {b, c, d} I (^) b → {a, d} I (^) c → {a} I (^) d → {a, b}

Raphaël Clifford [email protected]

Minimum Spanning Trees (MSTs)

Given an undirected connected graph, we reduce it so that there is exactly one path between any two nodes. By definition the resulting graph is a tree called the spanning tree. I (^) Number of edges = Number of vertices- I (^) Possibly very unbalanced I (^) Spanning trees are not unique Any (sensible) graph traversal will produce a spanning tree.

Raphaël Clifford [email protected]

Minimum Spanning Trees (MSTs)

A tree that connects all nodes, of minimum weight. (We assume all weights are distinct for simplicity. Full version in CLRS.)

a b

c d

e

f

g

h

Raphaël Clifford [email protected]

MSTs - Optimal substructure

A property of MSTs that will allow us to use a greedy algorithm to find one is called optimal substructure. Consider an MST T. Remove any edge (u, v ) ∈ T.

u

v

T 1

T 2

Then T is partitioned into two subtrees, T 1 and T 2.

Raphaël Clifford [email protected]

Optimal substructure

u

v

T 1

T 2

Theorem

The subtree T 1 is an MST of G 1 = (V 1 , E 1 ), the subgraph of G induced by the vertices of T 1 : V 1 = vertices of T 1 , E 1 = (x, y) ∈ E : x, y ∈ V 1. Similarly for T 2

Proof: (by contradiction) w(T ) = w(u, v ) + w(T 1 ) + w(T 2 ) If T 1 ′ has a lower weight spanning tree than T 1 then T ′^ = {(u, v )} ∪ T 1 ′ ∪ T 2 is a lower weight spanning tree than T. Raphaël Clifford [email protected]

MSTs-greedy property

A locally optimal choice is globally optimal

Theorem

Let T be the MST of G = (V , E), and let A ⊆ V. If (u, v ) ∈ E is the least-weight edge connecting A to V − A, then, (u, v ) ∈ T.

Proof: (by contradiction) Consider the nodes u ∈ A and v ∈ V − A. If the least-weight edge connecting them is not included in the current ST then swap it for the first edge on the path from u to v that connects A and V − A. The weight of the tree is now reduced.

Raphaël Clifford [email protected]

MSTs-greedy property

A locally optimal choice is globally optimal

Theorem

Let T be the MST of G = (V , E), and let A ⊆ V. If (u, v ) ∈ E is the least-weight edge connecting A to V − A, then, (u, v ) ∈ T.

Proof: (by contradiction) Consider the nodes u ∈ A and v ∈ V − A. If the least-weight edge connecting them is not included in the current ST then swap it for the first edge on the path from u to v that connects A and V − A. The weight of the tree is now reduced.

Raphaël Clifford [email protected]

Minimum Spanning Trees (MSTs)

IDEA: Maintain V − A as a priority queue Q. Key each vertex in Q with the weight of the least- weight edge connecting it to a vertex in A. I (^) Queue contains all vertices that are not in the tree. I (^) For each vertex, its key is the minimum weight of any edge connecting it to a vertex in the tree. I (^) A data structure which supports INSERT, FIND-MIN, EXTRACT-MIN and DECREASE-KEY is known as a priority queue Take the next vertex from the queue and put it in the tree. Update the keys. How can we efficiently implement the queue?

Raphaël Clifford [email protected]

Minimum Spanning Trees (MSTs)

IDEA: Maintain V − A as a priority queue Q. Key each vertex in Q with the weight of the least- weight edge connecting it to a vertex in A. I (^) Queue contains all vertices that are not in the tree. I (^) For each vertex, its key is the minimum weight of any edge connecting it to a vertex in the tree. I (^) A data structure which supports INSERT, FIND-MIN, EXTRACT-MIN and DECREASE-KEY is known as a priority queue Take the next vertex from the queue and put it in the tree. Update the keys. How can we efficiently implement the queue?

Raphaël Clifford [email protected]