lecture 11
Download
Skip this Video
Download Presentation
Lecture 11

Loading in 2 Seconds...

play fullscreen
1 / 62

Lecture 11 - PowerPoint PPT Presentation


  • 128 Views
  • Uploaded on

Lecture 11. Graph Algorithms. Definitions. Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). . An edge can connect two vertices. An edge between vertex u and v is denoted as (u, v).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 11' - roza


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture 11

Lecture 11

Graph Algorithms

definitions
Definitions

Graph is a set of vertices V, with edges connecting some of the vertices (edge set E).

An edge can connect two vertices.

An edge between vertex u and v is denoted as (u, v).

  • In a directed graph (digraph) all edges have directions. In an undirected graph, an edge does not have a direction.
    • Unless otherwise mentioned a graph is undirected
real life examples where graphs are useful

A

B

E

F

C

D

Real life examples where graphs are useful

Cities and Roads

  • Networks:
    • Routers are vertices
    • Links are edges
  • Shortest path between vertices.

Constraint representation:

If A is there, B can not be there, etc.

slide4

A

A

B

B

E

E

F

F

C

C

D

D

A vertex v is adjacent to vertex u, if there is an edge (u, v), e.g., A, B

In an undirected graph, existence of edge (u, v) means both u and v are adjacent to each other.

In a digraph, existence of edge (u, v) does not mean u is adjacent to v.

E is adjacent to B, but B is not adjacent to E

slide5

A

B

E

F

C

D

An edge may have a weight, e.g., distance of a highway

0.4

0.5

0.1

0.1

0.3

0.2

A path in a graph is a sequence of vertices w1 w2 …..wP such that consecutive vertices wi wi+1 have an edge between them, i.e., wj+1 is adjacent to wj

ABE is a path

slide6

A path in a graph is simple if all vertices are distinct, except possibly the first and the last one, e.g., ABE, but ABEB is not a simple path, unless otherwise stated paths are simple.

Length of a path is the number of edges in the path.

What is the length of a simple path of path ABE?

2

A cycle is a path of length at least 1 such that the first and the last vertices are equal. Example?

BCDEB

A cycle is simple if the path is simple, e.g., BCDEB

For undirected graph, we require a cycle to have distinct edges.

Length of a cycle is the number of edges in the cycle.

What is the length of a simple cycle of P vertices?

P

connected graphs

A

A

B

B

E

E

F

F

C

C

D

D

Connected Graphs

A graph is connected if there is a path from every vertex to every other vertex.

A directed graph with this property is strongly connected.

If a directed graph is not strongly connected, but underlying undirected graph is connected then the directed graph is weakly connected.

Connected

Weakly connected, cant go from E to A

subgraphs and components
Subgraphs and Components

A subgraph of a graph is a graph which has a subset of vertices and a subset of edges of the original graph.

  • A component is a subgraph which satisfies two properties
    • is connected, and
    • Maximal with respect to this property.

``Connected ‘’ will be replaced by ``strongly connected’’ for digraphs.

slide9

A

A

B

B

E

E

F

F

C

C

D

D

H

G

Entire graph is a component

Two components:

ABCDEF

GH

slide10

A

B

E

F

C

D

3 components

BCDE

A

F

complete graph

B

E

C

D

Complete Graph

A graph which has edge between any vertex pair is complete, e.g., BCDE

A complete digraph has directed edges between any two vertices.

How many edges can a complete graph of N vertices have? N(N-1)/2

How about a digraph? N(N-1)

representation of graphs
Representation of Graphs

Adjacency matrix:

Let there be N vertices,0, 1,….N-1

Declare a N x N array A(adjacency matrix)

A[j][k] = 1 if there is an edge (j, k)

= 0 otherwise

Storage?

O(N2)

slide13

A

B

E

F

C

D

Adjacency matrix:

A B C D E F

A 0 1 0 0 0 0

B 1 0 1 0 1 0

C 0 1 0 1 0 0

D 0 0 1 0 1 0

E 0 1 0 1 0 1

F 0 0 0 0 1 0

slide14

A

B

E

F

C

D

Adjacency matrix:

A B C D E F

A 0 1 0 0 0 0

B 0 0 1 0 1 0

C 0 0 0 1 0 0

D 0 0 0 0 1 0

E 0 0 0 0 0 1

F 0 0 0 0 0 0

slide15

What will be a the structure of A for an undirected graph?

A[j][k] = A[k][j]

symmetric

Weighted graph

A[j][k] = weight of edge (j, k) if edges have weights

= a very large or very small value if edge (j, k) does not exist

adjacency list
Adjacency List

If the graph is complete or almost complete, then adjacency matrix representation is fine, otherwise O(N2) storage is used even though there are fewer edges

Adjacency list is a more efficient storage.

Every vertex has a linked list of the vertices which are adjacent to it.

Read Section 9.1

slide17

A

B

E

F

C

D

A B

B A,C,E

C B,D

D C,E

E B,D,F

F E

slide18

A

B

E

F

C

D

A B

B C,E

C D

D E

E F

F

slide19

Storage: O(V+E)

However, problem with adjacency list is that one may have to traverse the entire link list corresponding to a vertex in order to locate an edge.

sparse and dense graphs
Sparse and Dense Graphs

Sparse graphs have (V) edges.

Dense graphs have (V2) edges.

(V)

What is the storage in adjacency list for sparse graphs?

dense graphs?

adjacency matrix for sparse graphs?

dense graphs?

(V2)

(V2)

(V2)

Adjacency list is better for sparse graphs, adjacency matrix for dense graphs

degree relations

A

B

E

F

C

D

Degree Relations

Number of edges incident from a vertex is the degree of the vertex in a graph.

1

Deg(A) ?

3

Deg(B) ?

Number of edges ending at a vertex is the indegree of the vertex in a digraph.

Number of edges originating from a vertex is the outdegree of the vertex in a digraph.

slide22

A

B

E

F

C

D

Indeg(A) ?

0

1

Indeg(B) ?

Outdeg(A) ?

1

2

Outdeg(B) ?

slide23

A

A

B

B

E

E

F

F

C

C

D

D

For a graph,

Sum of degrees of all vertices = 2. Number of edges

Sum of degrees ?

12

Number of edges ?

6

For a digraph, sum of indegrees of all vertices = sum of outdegrees of all vertices = sum of edges

Sum of indegrees ?

6

Sum of outdegrees ?

6

graph traversal
Graph Traversal

Breadth First Search

  • Starts from a vertex s (source), and discovers all vertices which are reachable from s
    • A vertex v is reachable from s if there is a path from s to v.

Start from a vertex v

Go to all vertices adjacent to v

For each of these adjacent vertices, go to all vertices adjacent to these

slide25

A

B

E

F

C

D

Start from C

Go to B, D

From B go to A, E

From E go to F

Vertices are discovered in order of increasing shortest path lengths from start vertex

Order of discovery starting from C: B,D,A,E,F

Shortest path lengths from C: B,D(1), A,E(2), F(3)

slide26

We use a FIFO queue in the search which is initially empty.

Initially all vertices are colored white.

When a vertex is ``discovered’’, it is colored gray and put in the FIFO queue

First source is discovered, then the vertices adjacent to source, etc.

Remove vertices from the queue in FIFO order, discover its undiscovered neighbors and put them in the FIFO queue

When all vertices adjacent to a vertex v have been discovered, the algorithm finishes processing v, and colors it black.

slide27

A

A

A

B

B

B

E

E

E

F

F

F

C

C

C

D

D

D

Discover source C, enqueue it

Queue = C

Dequeue C

Discover B, D, make them gray

Queue = B,D

Blacken C

Dequeue B, Discover A E, make them gray

Queue = D,A, E, Blacken B

Dequeue D, nothing new to discover

Queue = A,E, Blacken D

slide28

A

A

B

B

E

E

F

F

C

C

D

D

Dequeue A, Nothing new to discover

Queue = E, Blacken A

Dequeue E, Discover F, make it gray

Queue = F, Blacken E

Dequeue F, Nothing new to discover

Queue = empty, Blacken F

slide29

Notation:

d[u] is the length of the shortest path from s to u

color[u] is the color of vertex u

pred[u] is the predecessor of u in the search

BFS is used to measure the shortest paths by maintaining d[u]

BFS generates a tree by maintaining the predecessors

pred(C ) = Null, pred(B ) = pred(D ) = C ,

pred(A ) = B, pred(E ) = B, pred(F ) = E

pseudocode
Pseudocode

BFS(G,s)

{

For each v in V, {color[v]=white; d[u]= INFINITY; pred[u]=NULL}

color[s] = gray; d[s]=0;

Queue = {s};

While Queue is nonempty

{

u = Dequeue[Q];

slide31

For each v in Adj[u], {

if (color[v] = white) /*if v is discovered*/

{

color[v] = gray; /*Discover v*/

d[v] = d[u] + 1; /*Set distance of v*/

pred[v] = u; /*Set pred of v*/

Enqueue(v); /*put v in Queue*/

} }

Color[u] = black; /*done with u*/

}

}

Work out predecessors and distance labels in he previous example.

complexity analysis
Complexity Analysis
  • A vertex is visited once.
    • Thus the while loop is executed at most V times.

Complexity of operations inside the for loop is constant.

We want to compute the number of times the for loop is executed.

For each vertex v the for loop is executed at most (deg v + 1) times.

The factor 1 comes as for a 0 degree vertex we need a constant complexity

slide33

Thus the for loop is executed v (deg v + 1) times

This equals V + 2E

Initialization complexity is V

Thus overall we have complexity V + V + 2E, i.e. O(V+E)

depth first search
Depth First Search

Another graph traversal process.

We want to visit all rooms in a castle.

Start from a room

Move from room to room till you reach an undiscovered room

Draw a graffiti in each undiscovered room

Once you reach a discovered room take a door which you have not taken before.

slide35

A

B

E

F

C

D

Start from C

Go to B

From B go to A

Return to B

From B go to E

From E go to F

Return to E, B, C

Go to D

slide36

Will have 3 possible colors for a vertex:

white for an undiscovered vertex

gray for a discovered vertex

black for a finished vertex

Will store predecessor

Will store 2 numbers for each vertex (timestamps)

When we first discover a vertex store a counter d[u]

When you finish off store another f[u]

d[u] is not the distance

pseudocode1
Pseudocode

DFS(G)

{

For each v in V, {color[v]=white; pred[u]=NULL}

time=0;

For each u in V

If (color[u]=white) DFSVISIT(u)

}

time is a global variable

slide38

DFSVISIT(u)

{

color[u]=gray;

d[u] = ++time;

For each v in Adj(u) do

If (color[v] = white)

{

pred[v] = u;

DFSVISIT(v);

}

color[u] = black; f[u]=++time;

}

slide39

A

A

A

A

B

B

B

B

E

E

E

E

F

F

F

F

C

C

C

C

D

D

D

D

Discover source C, d[C] = 1

Discover B, make it gray

d[B] = 2

Discover A, make it gray

d[A] = 3

Finish A, f[A] = 4, Blacken A

Discover E, make it gray, d[E]=5

slide40

A

A

A

B

B

B

E

E

E

F

F

F

C

C

C

D

D

D

Discover F, d[F] = 6

Finish F, f[F]=7

Discover D, d[D]=8

Finish D, f[D]=9, Finish E f[E] = 10,

Finish B, f[B] = 11, Finish C, f[C] = 12

pred[B]=C, pred[A]=B, pred[E]=B, pred[F]=E, pred[D] = E

complexity analysis1
Complexity Analysis

Note down example from board

There is only one DFSVISIT(u) for each vertex u.

Let us analyze the complexity of a DFSVISIT(u)

Ignoring the recursion calls the complexity is O(deg(u)+1)

We consider the recursive calls in separate DFSVISIT(v)

Initialization complexity is O(V)

Overall complexity is O(V + E)

dfs tree structure

A

B

E

F

C

D

DFS Tree Structure

Consider a directed graph.

Observe that if u is predecessor of v in DFS, there is an edge (u, v) in the graph.All such edges are predecessor edges or tree edges.

The predecessor edges constitute an acyclic graph (DFS tree)

slide43

A

B

E

F

C

D

If there is a path from v to u in the DFS tree, then u is an ancestor of v in DFS tree and v is a descendant of u.

B is ancestor of F

F is descendant of B

slide44

A

B

E

F

A

C

C

D

B

An edge (u, v) in the graph such that v is an ancestor of u is a ``back edge’’, e.g., edge CD

An edge (u, v) where v is a direct descendant of u is a forward edge, e.g., BE

An edge (u, v) where u is not an ancestor nor descendant of v is a cross-edge.

Do cross edges exist in undirected graphs?

No

Are all edges either forward, cross and back?

No in digraph (e.g., AC), yes in graph

Other edges are called tree edges

slide45

2,5

1,6

3,4

7,10

8,9

A

C

B

D

E

DFS starts from A, order A,B,C,E,D

Forward edge: AB, BC, ED

Tree edge: AC

Cross edge: DB

Backward edge: CA

d[A] = 1, d[B] = 2, d[C]=3, f[C]=4, f[B]=5, f[A]=6, d[E]=7,d[D]=8,f[D]=9,f[E]=10

relation between timestamps and ancestry
Relation between timestamps and ancestry

u is an ancestor of v if and only if [d[u], f[u]] [d[v], f[v]]

u is a descendant of v if and only if [d[u], f[u]][d[v], f[v]]

u and v are not related if and only if [d[u], f[u]] and [d[v], f[v]] are disjoint

These relations are called parenthesis lemma

applications of dfs
Applications of DFS

DFS can be used to find out whether a graph or a digraph contains a cycle.

Consider a digraph. It has a cycle if and only if the graph has a back edge. The same holds for graphs.

Run DFS

Check the nature of every edge (How do you know whether an edge is a back edge or not?)

If there is a back edge, then the graph has a cycle.

Complexity?

O(V + E)

slide48

Now we show that a digraph has a cycle if and only if there is a back edge.

If there is a back edge there is a cycle.

We show that if there is a cycle, there is a back edge.

Consider an edge (u, v) in a digraph. If it is a back edge, then f[u] f[v]. Otherwise (for tree, forward, cross edges) f[u] > f[v]

We show it as follows. For tree, back and forward edges, the result follows intuitively as well as from the parenthesis lemma.

slide49

For cross edge (u, v) note that the intervals [d[u], f[u]] and [d[v], f[v]] are disjoint.

When we were processing u, v was not white otherwise (u, v) will be a predecessor edge

Thus processing v started before processing u.

Thus d[v] < d[u].

Since the intervals are disjoint, this means f[v] < f[u].

Now we show that if there is a cycle, there is a back edge.

slide50

Suppose there is no back edge.

Move along any path. All edges are tree forward or cross edges. Thus the finish times decrease monotonically.

Hence we don’t come back to the same vertex. Thus there is no cycle.

topological sort
Topological Sort

A DAG (directed acyclic graph) is a digraph without any cycle.

Topological sort of DAG is ordering the vertices such that if there is an edge (u, v) then u must come before v in the order.

Application: You have a set of tasks to be completed in a factory. There are relations between some tasks such that A must be finished before B begins (Example: To build second floor you must construct first floor first, but there is no relation between electrical wiring and plumbing).

slide52

We need to order the tasks.

We represent the tasks by vertices and there is an edge (u, v) if u must be finished before v begins.

Next we do a topological sort on them.

(There is something wrong with the task relations if the representation has a cycle. This can be detected by a DFS cycle detection. So we assume that the graph is a DAG).

Note that any ordering of the vertices, such that there is no edge from a vertex later in the order to another which is ahead in the order is a valid topological order.

slide53

Note that we have a DAG (no cycle). Thus there is no back edge.

Thus for any edge (u, v) f(u) > f(v).

Thus vertices ordered in decreasing order of their finish times has a topological order.

This can be attained as follows.

While running DFS, whenever a vertex is colored black add it to the front of a linked list.

Output the linked list at the end.

Complexity?

O(V+E)

pseudocode2
Pseudocode

Topological Sort(G)

{

For each v in V, {color[v]=white; pred[u]=NULL}

time=0;

Linked list = empty;

For each u in V

If (color[u]=white) DFSVISIT(u)

Output Linked list;

}

slide55

Topology-DFSVISIT(u)

{

color[u]=gray;

d[u] = ++time;

For each v in Adj(u) do

If (color[v] = white)

{

pred[v] = u;

DFSVISIT(v);

}

color[u] = black; f[u]=++time;

Add u to the end of a linked list.

}

slide56

2,5

1,6

3,4

7,10

8,9

Linked list = C

Go to C

Finish C

Linked list = B, C

Return to B

Finish B

Return to A

Finish A

Linked list = A,B,C

Go to D

Finish D

Linked list = D,A,B,C

Go to E

Finish E

Linked list = E, D,A,B,C

A

C

B

D

E

Start with A,

Go to B

strong components
Strong Components
  • A strong component of a digraph is a subgraph which is
    • Strongly connected
    • Maximal w.r.t. this property

How many strong components does a strongly connected digraph have?

1

A variant of DFS gives all strong components of a digraph

slide58

A

B

E

F

C

D

If we start DFS from any vertex in a strongly connected component, we will finish all other vertices in the strong component before finishing this vertex,

And possibly finish a few other vertices. (leaking into other components)

Strong components:

A, B,C,D,E, F

Start DFS from B, Cover C,D,E and leak to F

If there is no leaking, we are done!

If we know the strong components apriori, we can prevent leaking by choosing the DFS order properly, e.g., an order of selection E, B then A would yield the strong components

slide59

A

BCDE

F

Replace each strong component by a single vertex.

There is an edge from vertex A to vertex B if there is an edge from a vertex u in component A to another vertex v in component B.

The resulting graph is a DAG. Why?

Order the vertices of the new DAG in a reverse topological order (reverse order of the normal topological order).

Example: F, BCDE, A

Now, if you choose vertices in the above order, you are done

slide60

However, we don’t know the components ahead of time. But, the previous argument tells us that we need to use reverse topological order somehow.

The following actually works!

Run DFS

Sort the vertices in decreasing order of finish times

Reverse the digraph.

Run DFS. Each time you need to choose a vertex choose it in the sorted order.

Whenever you retrace to the main loop, you actually start a new strongly connected component.

slide61

A

A

B

B

E

E

F

F

C

C

D

D

First DFS order: A,B,C,D,E,F

Sorted order: A,B,C,D,E,F

Reversed Digraph

Choose A, Return to main loop

Choose B, Do E,D,C, return to main loop

Choose F, Return to main loop

Strong components are: A, BEDC, F

complexity analysis2
Complexity Analysis

What sort would you use?

Integer sort

What is the overall complexity?

O(V+E)

First DFS: O(V+E)

Sorting: O(V)

Reversing digraph: O(V+E)

Second DFS: O(V+E)

ad