Graph Algorithms (Chapter 10) Alexandre David B2-206 Today - - PowerPoint PPT Presentation

graph algorithms chapter 10
SMART_READER_LITE
LIVE PREVIEW

Graph Algorithms (Chapter 10) Alexandre David B2-206 Today - - PowerPoint PPT Presentation

Graph Algorithms (Chapter 10) Alexandre David B2-206 Today Recall on graphs. Minimum spanning tree (Prims algorithm). Single-source shortest paths (Dijkstras algorithm). All-pair shortest paths (Floyds algorithm).


slide-1
SLIDE 1

Graph Algorithms (Chapter 10)

Alexandre David B2-206

slide-2
SLIDE 2

28-04-2006 Alexandre David, MVP'06 2

Today

Recall on graphs. Minimum spanning tree (Prim’s algorithm). Single-source shortest paths (Dijkstra’s

algorithm).

All-pair shortest paths (Floyd’s algorithm). Connected components.

slide-3
SLIDE 3

28-04-2006 Alexandre David, MVP'06 3

Graphs – Definition

A graph is a pair (V,E )

V finite set of vertices. E finite set of edges.

e ∈ E is a pair (u,v ) of vertices. Ordered pair → directed graph. Unordered pair → undirected graph.

slide-4
SLIDE 4

28-04-2006 Alexandre David, MVP'06 4

edge vertex

V= E= V= E=

slide-5
SLIDE 5

28-04-2006 Alexandre David, MVP'06 5

Graphs – Edges

Directed graph:

(u,v ) ∈ E is incident from u and incident to v. (u,v ) ∈ E : vertex v is adjacent to u.

Undirected graph:

(u,v ) ∈ E is incident on u and v. (u,v ) ∈ E : vertices u and v are adjacent to

each other.

slide-6
SLIDE 6

28-04-2006 Alexandre David, MVP'06 6

4 adjacent to 6

slide-7
SLIDE 7

28-04-2006 Alexandre David, MVP'06 7

Graphs – Paths

A path is a sequence of adjacent vertices.

Length of a path = number of edges. Path from v to u ⇒ u is reachable from v. Simple path: All vertices are distinct. A path is a cycle if its starting and ending

vertices are the same.

Simple cycle: All intermediate vertices are

distinct.

slide-8
SLIDE 8

28-04-2006 Alexandre David, MVP'06 8

Simple path: Simple cycle: Non simple cycle: Simple path: Simple cycle: Non simple cycle:

slide-9
SLIDE 9

28-04-2006 Alexandre David, MVP'06 9

Graphs

Connected graph: ∃ path between any

pair.

G’=(V’,E’) sub-graph of G=(V,E) if V’⊆V

and E’⊆E.

Sub-graph of G induced by V’: Take all

edges of E connecting vertices of V’⊆V.

Complete graph: Each pair of vertices

adjacent.

Tree: connected acyclic graph.

slide-10
SLIDE 10

28-04-2006 Alexandre David, MVP'06 10

Sub-graph: Induced sub-graph:

slide-11
SLIDE 11

28-04-2006 Alexandre David, MVP'06 11

Graph Representation

Sparse graph (|E| much smaller than |V|2):

Adjacency list representation.

Dense graph:

Adjacency matrix.

For weighted graphs (V,E,w): weighted

adjacency list/matrix.

slide-12
SLIDE 12

28-04-2006 Alexandre David, MVP'06 12

⎩ ⎨ ⎧ ∈ =

  • therwise

E v v if a

j i j i

) , ( 1

,

Undirected graph ⇒ symmetric adjacency matrix. |V|

|V|2 entries

slide-13
SLIDE 13

28-04-2006 Alexandre David, MVP'06 13

|V|

|V|+|E| entries

slide-14
SLIDE 14

28-04-2006 Alexandre David, MVP'06 14

Minimum Spanning Tree

We consider undirected graphs. Spanning tree of (V,E) = sub-graph

being a tree and containing all vertices V.

Minimum spanning tree of (V,E,w) =

spanning tree with minimum weight.

Example: minimum length of cable to

connect a set of computers.

slide-15
SLIDE 15

28-04-2006 Alexandre David, MVP'06 15

Spanning Trees

slide-16
SLIDE 16

28-04-2006 Alexandre David, MVP'06 16

Prim’s Algorithm

Greedy algorithm:

Select a vertex. Choose a new vertex and edge guaranteed to

be in a spanning tree of minimum cost.

Continue until all vertices are selected.

slide-17
SLIDE 17

28-04-2006 Alexandre David, MVP'06 17

Vertices of minimum spanning tree. Weights from VT to V. select add update

slide-18
SLIDE 18

28-04-2006 Alexandre David, MVP'06 18

slide-19
SLIDE 19

28-04-2006 Alexandre David, MVP'06 19

slide-20
SLIDE 20

28-04-2006 Alexandre David, MVP'06 20

slide-21
SLIDE 21

28-04-2006 Alexandre David, MVP'06 21

Prim’s Algorithm

Complexity Θ(n2). Cost of the minimum spanning tree: How to parallelize?

Iterative algorithm. Any d[v] may change after every loop. But possible to run each iteration in parallel.

∈V v

v d ] [

slide-22
SLIDE 22

28-04-2006 Alexandre David, MVP'06 22

1-D Block Mapping

p processes n vertices n/p vertices per process

slide-23
SLIDE 23

28-04-2006 Alexandre David, MVP'06 23

Parallel Prim’s Algorithm

1-D block partitioning: Vi per Pi. For each iteration: Pi computes a local min di[u]. All-to-one reduction to P0 to compute the global min. One-to-all broadcast of u. Local updates of d[v]. Every process needs a column of the adjacency matrix to compute the update. Θ(n2/p) space per process.

slide-24
SLIDE 24

28-04-2006 Alexandre David, MVP'06 24

Analysis

The cost to select the minimum entry is

O(n/p + log p).

The cost of a broadcast is O(log p). The cost of local update of the d vector is

O(n/p).

The parallel run-time per iteration is

O(n/p + log p).

The total parallel time (n iterations) is

given by O(n2/p + n log p).

slide-25
SLIDE 25

28-04-2006 Alexandre David, MVP'06 25

Analysis

Efficiency = Speedup/# of processes:

E=S/p=1/(1+Θ((p logp)/n).

Maximal degree of concurrency = n. To be cost-optimal we can only use up to

n/logn processes.

Not very scalable.

slide-26
SLIDE 26

28-04-2006 Alexandre David, MVP'06 26

Single-Source Shortest Paths: Dijkstra’s Algorithm

For (V,E,w), find the shortest paths from a

vertex to all other vertices.

Shortest path=minimum weight path. Algorithm for directed & undirected with non

negative weights.

Similar to Prim’s algorithm.

Prim: store d[u] minimum cost edge

connecting a vertex of VT to u.

Dijkstra: store l[u] minimum cost to reach u

from s by a path in VT.

slide-27
SLIDE 27

28-04-2006 Alexandre David, MVP'06 27

Parallel formulation: Same as Prim’s algorithm.

slide-28
SLIDE 28

28-04-2006 Alexandre David, MVP'06 28

All-Pairs Shortest Paths

For (V,E,w), find the shortest paths

between all pairs of vertices.

Dijkstra’s algorithm: Execute the single-source

algorithm for n vertices → Θ(n3).

Floyd’s algorithm.

slide-29
SLIDE 29

28-04-2006 Alexandre David, MVP'06 29

All-Pairs Shortest Paths – Dijkstra – Parallel Formulation

Source-partitioned formulation: Each

process has a set of vertices and compute their shortest paths.

No communication, E=1, but maximal degree

  • f concurrency = n. Poor scalability.

Source-parallel formulation (p>n):

Partition the processes (p/n processes/subset),

each partition solves one single-source problem (in parallel).

In parallel: n single-source problems.

Up to n processes. Solve in Θ(n2 ).

Up to n2 processes, n2/ logn for cost-optimal, in which case solve in Θ(n logn).

slide-30
SLIDE 30

28-04-2006 Alexandre David, MVP'06 30

Floyd’s Algorithm

For any pair of vertices vi, vj ∈ V, consider

all paths from vi to vj whose intermediate vertices belong to the set {v1,v2,…,vk}.

Let pi,j

(k) (of weight di,j (k)) be the minimum-

weight path among them.

1 2 3 5 4 6 7 8

k i j

pi,j(k)

slide-31
SLIDE 31

28-04-2006 Alexandre David, MVP'06 31

Floyd’s Algorithm

If vertex vk is not in the shortest path from

vi to vj, then pi,j

(k) = pi,j (k-1).

1 2 3 5 4 6 7 8

k i j

pi,j(k)

k-1

=pi,j(k-1)

slide-32
SLIDE 32

28-04-2006 Alexandre David, MVP'06 32

Floyd’s Algorithm

If vk is in pi,j

(k), then we can break pi,j (k)

into two paths - one from vi to vk and one from vk to vj . Each of these paths uses vertices from {v1,v2,…,vk-1}.

1 2 3 5 4 6 7 8

k i j

pi,j(k) di,j(k)=di,k(k-1)+dk,j(k-1)

slide-33
SLIDE 33

28-04-2006 Alexandre David, MVP'06 33

Floyd’s Algorithm

Recurrence equation: Length of shortest path from vi to vj =

di,j

(n). Solution set = a matrix.

( )

⎭ ⎬ ⎫ ≥ = ⎪ ⎩ ⎪ ⎨ ⎧ + =

− − −

1 , min ) , (

) 1 ( , ) 1 ( , ) 1 ( , ) ( ,

k if k if d d d v v w d

k j k k k i k j i j i k j i

slide-34
SLIDE 34

28-04-2006 Alexandre David, MVP'06 34

Floyd’s Algorithm

Θ(n3)

Also works in place. How to parallelize?

slide-35
SLIDE 35

28-04-2006 Alexandre David, MVP'06 35

Parallel Formulation

2-D block mapping:

Each of the p processes has a sub-matrix

(n/√p)2 and computes its D(k).

Processes need access to the corresponding k

row and column of D(k-1).

kth iteration: Each processes containing part of

the kth row sends it to the other processes in the same column. Same for column broadcast

  • n rows.
slide-36
SLIDE 36

28-04-2006 Alexandre David, MVP'06 36

2-D Mapping

n/√p

slide-37
SLIDE 37

28-04-2006 Alexandre David, MVP'06 37

Communication

slide-38
SLIDE 38

28-04-2006 Alexandre David, MVP'06 38

Parallel Algorithm

slide-39
SLIDE 39

28-04-2006 Alexandre David, MVP'06 39

Analysis

E=1/(1+Θ((√p logp)/n). Cost optimal if up to O((n/ logn)2)

processes.

Possible to improve: pipelined 2-D block

mapping: No broadcast, send to

  • neighbour. Communication: Θ(n), up to

O(n2) processes & cost optimal.

slide-40
SLIDE 40

28-04-2006 Alexandre David, MVP'06 40

All-Pairs Shortest Paths: Matrix Multiplication Based Algorithm

Multiplication of the weighted adjacency

matrix with itself – except that we replace multiplications by additions, and additions by minimizations.

The result is a matrix that contains

shortest paths of length 2 between any pair of nodes.

It follows that An contains all shortest

paths.

slide-41
SLIDE 41

28-04-2006 Alexandre David, MVP'06 41

Serial algorithm not

  • ptimal but we can

use n3/logn processes to run in O(log2n).

slide-42
SLIDE 42

28-04-2006 Alexandre David, MVP'06 42

Transitive Closure

Find out if any two vertices are connected. G*=(V,E*) where E*={(vi,vj)|∃ a path

from vi to vj in G}.

slide-43
SLIDE 43

28-04-2006 Alexandre David, MVP'06 43

Transitive Closure

Start with D=(ai,j or ∞). Apply one all-pairs shortest paths

algorithm.

Solution:

⎪ ⎩ ⎪ ⎨ ⎧ = > ∞ = ∞ = j i

  • r

d if d if a

j i j i j i

1

, , * ,

slide-44
SLIDE 44

28-04-2006 Alexandre David, MVP'06 44

Connected Components

Connected components of G=(V,E) are the

maximal disjoint sets C1,…,Ck s.t. V=UCk and u,v ∈ Ci iff u reachable from v and v reachable from u.

slide-45
SLIDE 45

28-04-2006 Alexandre David, MVP'06 45

DFS Based Algorithm

DFS traversal of the graph → forest of

(DFS) spanning trees.

slide-46
SLIDE 46

28-04-2006 Alexandre David, MVP'06 46

slide-47
SLIDE 47

28-04-2006 Alexandre David, MVP'06 47

Parallel Formulation

Partition G into p sub-graphs. Pi has

Gi=(V,Ei).

Each Pi computes the spanning forest of Gi. Merge the forests pair-wise.

Each merge possible in Θ(n).

Not described in the book – out of scope. Find if an edge of A has its vertices in B:

no for all → union of 2 disjoint sets. yes for one → no union.

slide-48
SLIDE 48

28-04-2006 Alexandre David, MVP'06 48

Partition the adjacency matrix. 1-D partitioning in p stripes of n/p consecutive rows.

slide-49
SLIDE 49

28-04-2006 Alexandre David, MVP'06 49

P1 P2

slide-50
SLIDE 50

28-04-2006 Alexandre David, MVP'06 50

Analysis

E=1/(1+Θ((p logp)/n). Up to O(n/ logn) to be cost-optimal. Performance similar to Prim’s algorithm.