assignment 2 remarking

Post on 09-Jan-2016

39 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Assignment 2 Remarking. 2. y = 0.0323x. - 1.5352x + 47.927. Assignment 2 Marks. 2. R. = 0.9999. 100. 90. 80. 70. 60. New Mark. 50. 40. 30. 20. 10. 0. 0. 20. 40. 60. 80. Old Mark. Section V. Graph Algorithms. - PowerPoint PPT Presentation

TRANSCRIPT

COSC 3101N J. Elder

Assignment 2 Remarking

Assignment 2 Marksy = 0.0323x2 - 1.5352x + 47.927

R2 = 0.9999

0

10

20

30

40

50

60

70

80

90

100

0 20 40 60 80

Old Mark

New

Mar

k

COSC 3101N J. Elder

Section V. Graph Algorithms

COSC 3101N J. Elder

Directed and Undirected Graphs

(c) The subgraph of the graph in part (a) induced by the vertex set {1,2,3,6}.

(a) A directed graph G = (V, E), where V = {1,2,3,4,5,6} and E = {(1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3)}. The edge (2,2) is a self-loop.

(b) An undirected graph G = (V,E), where V = {1,2,3,4,5,6} and E = {(1,2), (1,5), (2,5), (3,6)}. The vertex 4 is isolated.

COSC 3101N J. Elder

Graph Isomorphism

COSC 3101N J. Elder

Trees

COSC 3101N J. Elder

Running Time of Graph Algorithms

Running time often a function of both |V| and |E|.

For convenience, drop the | . | in asymptotic notation, e.g. O(V+E).

COSC 3101N J. Elder

Representations: Undirected Graphs

Adjacency List Adjacency Matrix

Space complexity:

Time to find all neighbours of vertex :u

Time to determine if ( , ) : u v E

( )V E

(degree( ))u

(degree( ))u

2( )V

( )V

(1)

COSC 3101N J. Elder

Adjacency List Adjacency Matrix

Space complexity:

Time to find all neighbours of vertex :u

Time to determine if ( , ) : u v E

( )V E

(degree( ))u

(degree( ))u

2( )V

( )V

(1)

Representations: Directed Graphs

COSC 3101N J. Elder

Breadth-First Search

Idea: send out search ‘wave’ from s.

Keep track of progress by colouring vertices: Undiscovered vertices are coloured white

Just discovered vertices (on the wavefront) are coloured grey.

Previously discovered vertices (behind wavefront) are coloured black.

Graph ( , ) (directed or undirected) aI nput: source vertex nd .V E sG V

Ouput:

[ ] distance f rom to , .

[ ] such that ( , ) is last edge on shortest path f rom to .

d v s v v V

v u u v s v

COSC 3101N J. Elder

Breadth-First Search Algorithm

Each vertex assigned finite d value at most once.

d values assigned are monotonically increasing over time.

Q contains vertices with d values {i, …, i, i+1, …, i+1}

Time = O(V+E)

COSC 3101N J. Elder

Loop Invariant

[ ] distance f rom to f or black and grey nodes, f or white nodes.d u s u

consists of grey verticesQ

White vertices are never adjacent to black vertices.

COSC 3101N J. Elder

Progress?

On every step at least one vertex turns black.

COSC 3101N J. Elder

Termination + LI Correctness

There are no gray nodes.

All the black nodes form a connected component in G: WHY?

For all the black nodes u, d[u] holds the distance from s to u.

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Recovering shortest path to node

COSC 3101N J. Elder

Colours are actually not required

COSC 3101N J. Elder

Depth First Search (DFS)

Idea:

Continue searching “deeper” into the graph, until we get stuck.

If all the edges leaving v have been explored we “backtrack” to the vertex from which v was discovered.

COSC 3101N J. Elder

Depth-First Search

Explore every edge, starting from different vertices if necessary.

As soon as vertex discovered, explore from it.

Keep track of progress by colouring vertices: White: undiscovered vertices

Grey: discovered, but not finished (still exploring from it)

Black: finished (found everything reachable from it).

Graph ( , ) (directed or uI nput ndire: c ) tedG V E

2 timestamps on each vertex:

[ ] discovery time.

[ ] fi nishing time.

Ouput:

d v

f v 1 [ ] [ ] 2| |d v f v V

COSC 3101N J. Elder

Depth-First Search Algorithm

total work = | [ ] | ( )v V

Adj v E

total work = ( )V

Thus running time = ( )V E

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

For any two vertices u and v, exactly one of the following holds:

the intervals [d[u], f[u]] and [d[v], f[v]] are disjoint, and neither u nor v is a descendant of the other.

the interval [d[u], f[u]] is contained entirely within the interval [d[v], f[v]], and u is a descendant of v.

the interval [d[v], f[v]] is contained entirely within the interval [d[u], f[u]], and v is a descendant of u.

The Parenthesis Theorem

COSC 3101N J. Elder

Corollary

Vertex v is a proper descendant of vertex u in the depth-first forest if and only if

d[u] < d[v] < f[v] < f[u]

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Classification of Edges in DFS

There are four edge types:

1. Tree edges are edges in the depth-first forest Gπ. Edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v).

2. Back edges are those edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree. Self-loops, which may occur in directed graphs, are considered to be back edges.

3. Forward edges are nontree edges (u, v) connecting a vertex u to a descendant v in a depth-first tree.

4. Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees.

COSC 3101N J. Elder

Example

COSC 3101N J. Elder

Classification of edges

When the edge e=(u,v) is first explored: If v is white – e is a tree edge.

If v is gray – e is a back edge.

If v is black – e is a forward or a cross edge.

COSC 3101N J. Elder

For Undirected Graphs

In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge.

COSC 3101N J. Elder

Strongly Connected Components

Given directed graph ( , )

A (SCC) of G

is a maximal set of vertices such that

, , is reachable f rom and vice-ver

strongly connected compone t

sa.

n

G V E

C V

u v C v u

COSC 3101N J. Elder

Computing Strongly Connected Components

Defi ne of G:T transposeG

( , ), {( , ) : ( , ) }T T TG V E E u v v u E

i.e. is with edges reversed.TG G

Note that and have the same SCCsTG G

COSC 3101N J. Elder

Component Graph

( , )SCC SCC SCCG V E

has one vertex f or each SCC in .SCCV G

has an edge if there is an edge between the corresponding SCCs in .SCCE G

G

SCCG

COSC 3101N J. Elder

Lemma

is a DAG.SCCG

I n other words,

Let and be distinct SCCs in C C G

Let , , ,u v C u v C

Suppose there is a path f rom to in .u u G

Then there cannot also be a path f rom to in .v v G

COSC 3101N J. Elder

SCC Algorithm

COSC 3101N J. Elder

Example

G

TG

1

23

4

56

1

2 34

COSC 3101N J. Elder

Notation

Extend notation f or and to sets of vertices :d f U V

( ) min{ [ ]} (earliest discovery time)

u Ud U d u

( ) max{ [ ]} (latest fi nishing time)

u Uf U f u

COSC 3101N J. Elder

Lemma

Let and be distinct SCCs in ( , ). C C G V E

Suppose there is an edge ( , ) such that and .u v E u C v C

Then ( ) ( ).f C f C

Similarly,

Suppose there is an edge ( , ) such that and .Tu v E u C v C

Then ( ) ( ).f C f C

COSC 3101N J. Elder

So why does the SCC Algorithm work?

GC Cu v

TGC Cu v

The second DFS on starts with SCC such that ( ) is maximum.TG C f C

Since ( ) ( ) , there are no edges f rom to in .Tf C f C C C C C G

Thus DFS will visit only vertices in .C

The next root chosen in the second DFS is in SCC such that f (C ) is maximum

over all SCCs other than .

C

C

DFS visits all vertices in , but the only edges out of go to , which we've already visited.C C C

COSC 3101N J. Elder

Minimum Spanning Trees

Example Problem You are planning a new terrestrial telecommunications network to

connect a number of remote mountain villages in a developing country.

The cost of building a link between pairs of neighbouring villages (u,v) has been estimated: w(u,v).

You seek the minimum cost design that ensures each village is connected to the network.

The solution is called a minimum spanning tree.

COSC 3101N J. Elder

Properties of a Minimum Spanning Tree

|V|-1 edges

Acyclic

Not necessarily unique

COSC 3101N J. Elder

Building the Minimum Spanning Tree

Iteratively construct the set of edges A in the MST.

Initialize A to {}

As we add edges to A, maintain a Loop Invariant: A is a subset of some MST

Maintain loop invariant and make progress by only adding safe edges.

An edge (u,v) is called safe for A iff A{u,v}) is also a subset of some MST.

COSC 3101N J. Elder

Generic MST Algorithm

Loop Invariant: A is a subset of some MST

Initialization?

Maintenance?

Termination?

COSC 3101N J. Elder

Finding a safe edge

Idea: Every 2 disjoint subsets of vertices must be connected by at least one edge.

Which one should we choose?

COSC 3101N J. Elder

Some definitions

A cut (S,V-S) is a partition of vertices into disjoint sets V and S-V.

Edge (u,v)E crosses cut (S, V-S) if one endpoint is in S and the other is in V-S.

A cut respects A iff no edge in A cross the cut.

An edge is a light edge crossing a cut iff its weight is minimum over all edges crossing the cut.

COSC 3101N J. Elder

Theorem

Let A be a subset of some MST

(S,V-S) be a cut that respects A

(u,v) be a light edge crossing (S,V-S)

Then (u,v) is safe for A.

COSC 3101N J. Elder

Proof

Let T be an MST that includes A.

If T contains (u,v) then we’re done.

Suppose T does not contain (u,v) Can construct different MST T' that includes A{u,v})

All edges shown (except (u,v)) are in T

COSC 3101N J. Elder

In Generic-MST

A is a forest containing connected components.

Initially each component is a single vertex.

Any safe edge merges two of these components into one.

Each component is a tree.

After adding |V|-1 safe edges, only one component remains.

COSC 3101N J. Elder

Kruskal’s Algorithm for computing MST

Starts with each vertex being its own component.

Repeatedly merges two components into one by choosing the light edge that crosses the cut between them.

Scans the set of edges in monotonically increasing order by weight.

Uses a disjoint-set data structure to determine whether an edge connects vertices in different components.

COSC 3101N J. Elder

Kruskal’s Algorithm for computing MST

Example!

Running Time =

O(ElogE)

= O(ElogV)

COSC 3101N J. Elder

Example:

Kruskal’s Algorithm

COSC 3101N J. Elder

Prim’s Algorithm

Build one tree

Start from arbitrary root r

At each step, add light edge crossing cut (VA, V- VA), where VA = vertices that A is incident on.

COSC 3101N J. Elder

Finding light edges quickly

Use priority queue Each object is a vertex v in V-VA

Key of v is minimum weight of any edge (u,v), u VA.

Each vertex knows its parent in tree by [v].

As algorithm progresses, {( , [ ]) : { } }.A v v v V r Q

COSC 3101N J. Elder

Prim’s Algorithm

Example!

Running Time =

O(Elog(V))

top related