assignment 2 remarking

74
COSC 3101N J. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.0323x 2 - 1.5352x + 47.927 R 2 = 0.9999 0 10 20 30 40 50 60 70 80 90 100 0 20 40 60 80 Old Mark New Mark

Upload: jania

Post on 09-Jan-2016

39 views

Category:

Documents


0 download

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

Page 1: Assignment 2 Remarking

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

Page 2: Assignment 2 Remarking

COSC 3101N J. Elder

Section V. Graph Algorithms

Page 3: Assignment 2 Remarking

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.

Page 4: Assignment 2 Remarking

COSC 3101N J. Elder

Graph Isomorphism

Page 5: Assignment 2 Remarking

COSC 3101N J. Elder

Trees

Page 6: Assignment 2 Remarking

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

Page 7: Assignment 2 Remarking

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)

Page 8: Assignment 2 Remarking

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

Page 9: Assignment 2 Remarking

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

Page 10: Assignment 2 Remarking

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)

Page 11: Assignment 2 Remarking

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.

Page 12: Assignment 2 Remarking

COSC 3101N J. Elder

Progress?

On every step at least one vertex turns black.

Page 13: Assignment 2 Remarking

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.

Page 14: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 15: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 16: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 17: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 18: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 19: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 20: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 21: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 22: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 23: Assignment 2 Remarking

COSC 3101N J. Elder

Recovering shortest path to node

Page 24: Assignment 2 Remarking

COSC 3101N J. Elder

Colours are actually not required

Page 25: Assignment 2 Remarking

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.

Page 26: Assignment 2 Remarking

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

Page 27: Assignment 2 Remarking

COSC 3101N J. Elder

Depth-First Search Algorithm

total work = | [ ] | ( )v V

Adj v E

total work = ( )V

Thus running time = ( )V E

Page 28: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 29: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 30: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 31: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 32: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 33: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 34: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 35: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 36: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 37: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 38: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 39: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 40: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 41: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 42: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 43: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 44: Assignment 2 Remarking

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

Page 45: Assignment 2 Remarking

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]

Page 46: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 47: Assignment 2 Remarking

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.

Page 48: Assignment 2 Remarking

COSC 3101N J. Elder

Example

Page 49: Assignment 2 Remarking

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.

Page 50: Assignment 2 Remarking

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.

Page 51: Assignment 2 Remarking

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

Page 52: Assignment 2 Remarking

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

Page 53: Assignment 2 Remarking

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

Page 54: Assignment 2 Remarking

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

Page 55: Assignment 2 Remarking

COSC 3101N J. Elder

SCC Algorithm

Page 56: Assignment 2 Remarking

COSC 3101N J. Elder

Example

G

TG

1

23

4

56

1

2 34

Page 57: Assignment 2 Remarking

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

Page 58: Assignment 2 Remarking

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

Page 59: Assignment 2 Remarking

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

Page 60: Assignment 2 Remarking

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.

Page 61: Assignment 2 Remarking

COSC 3101N J. Elder

Properties of a Minimum Spanning Tree

|V|-1 edges

Acyclic

Not necessarily unique

Page 62: Assignment 2 Remarking

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.

Page 63: Assignment 2 Remarking

COSC 3101N J. Elder

Generic MST Algorithm

Loop Invariant: A is a subset of some MST

Initialization?

Maintenance?

Termination?

Page 64: Assignment 2 Remarking

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?

Page 65: Assignment 2 Remarking

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.

Page 66: Assignment 2 Remarking

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.

Page 67: Assignment 2 Remarking

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

Page 68: Assignment 2 Remarking

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.

Page 69: Assignment 2 Remarking

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.

Page 70: Assignment 2 Remarking

COSC 3101N J. Elder

Kruskal’s Algorithm for computing MST

Example!

Running Time =

O(ElogE)

= O(ElogV)

Page 71: Assignment 2 Remarking

COSC 3101N J. Elder

Example:

Kruskal’s Algorithm

Page 72: Assignment 2 Remarking

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.

Page 73: Assignment 2 Remarking

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

Page 74: Assignment 2 Remarking

COSC 3101N J. Elder

Prim’s Algorithm

Example!

Running Time =

O(Elog(V))