55
CS235102 CS235102 Data Structures Data Structures Chapter 6 Graphs Chapter 6 Graphs

CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline The Graph Abstract Data Type Graph Representations Elementary Graph Operations

  • View
    243

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

CS235102 CS235102 Data StructuresData Structures

Chapter 6 GraphsChapter 6 Graphs

Page 2: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Chapter 6 Graphs: OutlineChapter 6 Graphs: Outline The Graph Abstract Data TypeThe Graph Abstract Data Type

Graph RepresentationsGraph Representations

Elementary Graph OperationsElementary Graph Operations Death First and Breadth First SearchDeath First and Breadth First Search Spanning TreeSpanning Tree

Minimum Cost Spanning TreesMinimum Cost Spanning Trees Kruskal’s, Prim’s and Sollin’s AlgorithmKruskal’s, Prim’s and Sollin’s Algorithm

Shortest PathsShortest Paths Transitive ClosureTransitive Closure

Topological SortsTopological Sorts Activity NetworksActivity Networks Critical PathsCritical Paths

Page 3: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (1/13)The Graph ADT (1/13) IntroductionIntroduction

A graph problem example: Köenigsberg bridge problemA graph problem example: Köenigsberg bridge problem

Page 4: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (2/13)The Graph ADT (2/13) DefinitionsDefinitions

AA graph GG consists of two sets consists of two sets a finite, nonempty set of vertices a finite, nonempty set of vertices VV((GG)) a finite, possible empty set of edges a finite, possible empty set of edges EE((GG))

GG((VV,,EE) represents a graph) represents a graph AnAn undirected graph is one in which the pair of is one in which the pair of

vertices in an edge is unordered, vertices in an edge is unordered, ((vv00, , vv11) = () = (vv11,,vv00) ) A A directed graph is one in which each edge is a is one in which each edge is a

directed pair of vertices, directed pair of vertices, <<vv00, , vv11> != <> != <vv11, , vv00>>

tail head

Page 5: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (3/13)The Graph ADT (3/13) Examples for GraphExamples for Graph

complete undirected graph: n(n-1)/2 edges complete directed graph: n(n-1) edges

0

1 2

3G1

complete graph

0

1 2

3 4 5 6G2

0

1

2G3

incomplete graphV(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}

0

1 2

3

Page 6: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (4/13)The Graph ADT (4/13) Restrictions on graphsRestrictions on graphs

A graph may not have an edge from a vertex, A graph may not have an edge from a vertex, ii, , back tback to itselfo itself. Such edges are known as . Such edges are known as self loopsself loops

A graph may not have multiple occurrences of the saA graph may not have multiple occurrences of the same edge. If we remove this restriction, we obtain a dame edge. If we remove this restriction, we obtain a data referred to as a ta referred to as a multigraphmultigraph

feedback loops multigraph

Page 7: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (5/13)The Graph ADT (5/13) Adjacent and IncidentAdjacent and Incident If (If (vv00, , vv11) is an edge in an undirected graph, ) is an edge in an undirected graph,

vv00 and and vv11 are are adjacent The edge (The edge (vv00, , vv11) is ) is incident on vertices on vertices vv00 and and vv11

If <If <vv00, , vv11> is an edge in a directed graph> is an edge in a directed graph vv00 is is adjacent to vv11, and , and vv11 is is adjacent from vv00

The edge <The edge <vv00, , vv11> is incident on > is incident on vv00 and and vv11

V0 V1

V0 V1

Page 8: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (6/13)The Graph ADT (6/13) A subgraph of A subgraph of GG is a graph is a graph GG’ such that V(’ such that V(GG’) ’) V V

((GG) and E() and E(GG’) ’) E( E(GG).).

0

1 2

3G1 0

1

2G3

Page 9: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (7/13)The Graph ADT (7/13) PathPath

AA path from vertex from vertex vvpp to vertex to vertex vvqq in a graph in a graph GG, is a se, is a sequence of vertices, quence of vertices, vvpp, , vvii11, , vvii22, ..., , ..., vviinn, v, vqq, such that (, such that (vvpp, , vvii

11), (), (vvii11, , vvii22), ..., (), ..., (vviinn, v, vqq) are edges in an undirected graph.) are edges in an undirected graph. A path such as (0, 2), (2, 1), (1, 3) is also written as 0, 2, 1, 3A path such as (0, 2), (2, 1), (1, 3) is also written as 0, 2, 1, 3

TheThe length of a path is the number of edges on itis the number of edges on it

0

1 2

3

0

1 2

3

0

1 2

3

Page 10: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (8/13)The Graph ADT (8/13) Simple path and cycleSimple path and cycle

simple path (simple directed path):: a path in a path in which all vertices, except possibly the first and which all vertices, except possibly the first and the last, are distinct.the last, are distinct.

AA cycle is a simple path in which the first and is a simple path in which the first and the last vertices are the same.the last vertices are the same.

0

1 2

3

Page 11: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (9/13)The Graph ADT (9/13) Connected graphConnected graph

In an undirected graph G, twoIn an undirected graph G, two vertices, , vv00 and and vv11, are, are connected if there is a path in if there is a path in GG from from vv00 to to vv11

An undirected graph isAn undirected graph is connected if, for every pair of if, for every pair of distinct vertices distinct vertices vvii, , vvjj, there is a path from , there is a path from vvii to to vvjj

Connected componentConnected component AA connected component of an undirected graph is a of an undirected graph is a

maximal connected maximal connected subgraph.subgraph.

AA tree is a graph is a graph that is connected that is connected andand acyclic (i.e, (i.e, has no cycle)has no cycle)..

Page 12: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (10/13)The Graph ADT (10/13) Strongly Connected ComponentStrongly Connected Component

A directed graph isA directed graph is strongly connected if there is a dirif there is a directed path from ected path from vvii to to vvjj and also from and also from vvjj to to vvii

AA strongly connected component is a maximal subgrais a maximal subgraph that is strongly connectedph that is strongly connected

2

0

1

0

1

2G3 not strongly connected

strongly connected component(maximal strongly connected subgraph)

Page 13: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (11/13)The Graph ADT (11/13) DegreeDegree

TheThe degree of a vertex is the number of edges incident to tof a vertex is the number of edges incident to that vertex.hat vertex.

For directed graphFor directed graph in-degree ((vv) : the number of edges that have ) : the number of edges that have vv as the hea as the hea

dd out-degree ((vv) : the number of edges that have ) : the number of edges that have vv as the tail as the tail

If If ddii is the degree of a vertex is the degree of a vertex ii in a graph in a graph GG with with nn ver vertices and tices and ee edges, the number of edges is edges, the number of edges is

e di

n

( ) /0

1

2

Page 14: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT (12/13)The Graph ADT (12/13) Degree (cont’d)Degree (cont’d) We shall refer to a directed graph as a We shall refer to a directed graph as a digraphdigraph. .

When we us the term When we us the term graphgraph, we assume that it is , we assume that it is an undirected graphan undirected graph

undirected graph

degree

in-degree & out-degree

0

1

2

G3

in:1, out: 1

in: 1, out: 2

in: 1, out: 0

directed graph

3

0

1 2

3

33

3

G1

0

1 2

3 4 5 6

2

3 3

1 1 1

G2

1

Page 15: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

The Graph ADT The Graph ADT (13/13)(13/13) Abstract Data Type Abstract Data Type GraphGraph

Page 16: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (1/13)Graph Representations (1/13) Adjacency MatrixAdjacency Matrix Adjacency ListsAdjacency Lists Adjacency MultilistsAdjacency Multilists

Page 17: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (2/13)Graph Representations (2/13) Adjacency MatrixAdjacency Matrix

Let Let GG = ( = (VV,,EE) be a graph with ) be a graph with nn vertices. vertices. TheThe adjacency matrix of of GG is a two-dimensional is a two-dimensional

nn x x nn array, say array, say adj_matadj_mat If the edge (If the edge (vvii, , vvjj) ) is(not) in (not) in EE((GG), ), adj_mat[i][j]=adj_mat[i][j]=1(0)(0) The adjacency matrix for an undirected graph is symThe adjacency matrix for an undirected graph is sym

metric; the adjacency matrix for a digraph metric; the adjacency matrix for a digraph need not be symmetricneed not be symmetric

0

1

1

1

1

0

1

1

1

1

0

1

1

1

1

0

G1

0

1

0

0

0

1

0

1

0

G3

0

1

0

0

0

0

0

0

1

0

1

0

0

0

0

0

0

1

0

1

0

0

0

0

0

0

1

0

0

0

0

0

0

0

0

0

0

1

1

0

0

0

0

0

1

0

0

1

0

0

0

0

1

0

0

1

0

0

0

0

0

1

1

0

G4

0

1 23

0

1

2

1

0

2

3

4

5

6

7

Page 18: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (3/13)Graph Representations (3/13) Merits of Adjacency Matrix Merits of Adjacency Matrix

For an undirected graph, the degree of any vertex, For an undirected graph, the degree of any vertex, ii, , is its is its row sumrow sum::

For a directed graph, the For a directed graph, the row sumrow sum is the is the out-degreeout-degree, , while the while the column sumcolumn sum is the is the in-degreein-degree..

The complexity of checking edge number or The complexity of checking edge number or examining if examining if GG is connect is connect GG is undirected: is undirected: O(n2/2) GG is directed: is directed: O(n2)

1

0

]][[_n

j

jimatadj

ind vi A j ij

n

( ) [ , ]

0

1

outd vi A i jj

n

( ) [ , ]

0

1

Page 19: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (4/13)Graph Representations (4/13) Adjacency listsAdjacency lists

There is one list for each vertex in There is one list for each vertex in GG. The nodes in list . The nodes in list ii represent the vertices that are adjacent from vertex represent the vertices that are adjacent from vertex ii

For an undirected graph with For an undirected graph with nn vertices and vertices and ee edges, edges, this representation requires this representation requires nn head nodes and 2 head nodes and 2ee list list nodesnodes

CC declarations for adjacency lists declarations for adjacency lists

Page 20: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (5/13)Graph Representations (5/13) ExampleExample

G1

0

1 23

0

1

2

G3

1

0

2

3

4

5

6

7

G4

Page 21: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (6/13)Graph Representations (6/13) Sequential Representation of GrSequential Representation of Gr

aphaph Sequentially pack the nodes on the adjaSequentially pack the nodes on the adja

cency listscency lists nodenode[0] ~ [0] ~ nodenode[[nn-1] gives the starting -1] gives the starting

point of the list for vertex point of the list for vertex i i , 0≦, 0≦iinn nodenode[[nn] = ] = nn+2+2ee+1+1 The vertices adjacent from vertex The vertices adjacent from vertex ii are are

stored in stored in nodenode[[nodenode[[ii]]], … , ], … , nodenode[[nodenode[[ii+1]-1+1]-1], 0≦], 0≦iinn

The number of edges in The number of edges in GG may be may be determined in determined in OO((nn++ee) time) time

[0] 9 [12] 3

[1] 11 [13] 0 2

[2] 13 [14] 3

[3] 15 [15] 1 3

[4] 17 [16] 2

[5] 18 [17] 5 4

[6] 20 [18] 4 5

[7] 22 [19] 6

[8] 23 [20] 5 6

[9] 1 0 [21] 7

[10] 2 [22] 6 7

[11] 0 1

Page 22: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (7/13)Graph Representations (7/13)

Interesting OperationsInteresting Operations degree of a vertex in an undirected graph

# of nodes in adjacency list

# of edges in a graph determined in O(n+e)

out-degree of a vertex in a directed graph # of nodes in its adjacency list

in-degree of a vertex in a directed graph traverse the whole data structure

Page 23: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (8/13)Graph Representations (8/13) Finding In-degree of VerticesFinding In-degree of Vertices

0

1

2

1 NULL

0 NULL

1 NULL

0

1

2

tail head column link for head row link for tail

Inverse adjacency list of G3

Alternate node structure of adjacency lists

Page 24: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (9/13)Graph Representations (9/13) Example of Changing Node StructureExample of Changing Node Structure

0 1 2

2 NULL

1

0

1 0 NULL

0 1 NULL NULL

1 2 NULL NULL 0

1

2

0

1

0

0

0

1

0

1

0

Orthogonal representation of G3

Page 25: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (10/13)Graph Representations (10/13)

Vertices in Any OrderVertices in Any Order

3 2 NULL 1 0

2 3 NULL 0 1

3 1 NULL 0 2

2 0 NULL 1 3

Head nodes vertex links

0

1 2

3

Order is of no significance

Page 26: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (11/13)Graph Representations (11/13)

Adjacency MultilistsAdjacency Multilists Lists in which nodes may be shared among several lisLists in which nodes may be shared among several lis

ts. ts. (an edge is shared by two different paths) There is exactly one node for each edge. There is exactly one node for each edge. This node is on the adjacency list for each of the two This node is on the adjacency list for each of the two

vertices it is incident tovertices it is incident to

Page 27: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (12/13)Graph Representations (12/13) Example for Adjacency MultlistsExample for Adjacency Multlists

0

1 2

3

Page 28: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Representations (13/13)Graph Representations (13/13)

Weighted edgesWeighted edges The edges of a graph have weights assigned to them.The edges of a graph have weights assigned to them. These weights may represent asThese weights may represent as

the distance from one vertex to anotherthe distance from one vertex to another cost of going from one vertex to an adjacent vertex.cost of going from one vertex to an adjacent vertex.

adjacency matrix: adjacency matrix: adj_matadj_mat[[ii][][jj] would keep the weights] would keep the weights.. adjacency lists: add a adjacency lists: add a weightweight field field to the node structur to the node structur

e.e. A graph with weighted edges is called a A graph with weighted edges is called a networknetwork

Page 29: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (1/20)Graph Operations (1/20)

TraversalTraversalGiven Given GG=(=(VV,,EE) and vertex ) and vertex vv, find all , find all wwVV, such t, such that hat ww connects connects vv Depth First Search (DFS):Depth First Search (DFS): preorder traversal preorder traversal Breadth First Search (BFS):Breadth First Search (BFS): level order traversal level order traversal

Spanning TreesSpanning Trees Biconnected ComponentsBiconnected Components

Page 30: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (2/20)Graph Operations (2/20) Example for traversal Example for traversal

(using Adjacency List representation of Graph)(using Adjacency List representation of Graph)depth first search (DFS): v0, v1, v3, v7, v4, v5, v2, v6

breadth first search (BFS): v0, v1, v2, v3, v4, v5, v6, v7

Page 31: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Depth First SearchDepth First Search

#define FALSE 0#define TRUE 1short int visited[MAX_VERTICES];

Data structureadjacency list: O(e)adjacency matrix: O(n2)

w

[0] [1] [2] [3] [4] [5] [6] [7]

output:

0 1 3 7 4 5 2 6

visited:

Page 32: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (4/20)Graph Operations (4/20)

Breadth First SearchBreadth First Search It needs a queue to implement breadth-first searchIt needs a queue to implement breadth-first search void bfs(int v): breadth first traversal of a graphvoid bfs(int v): breadth first traversal of a graph

starting with node v the global array visited is initialized to 0starting with node v the global array visited is initialized to 0 the queue operations are similar to those described in Chaptthe queue operations are similar to those described in Chapt

er 4er 4

typedef struct queue *queue_pointer;typedef struct queue { int vertex; queue_pointer link; };void addq(queue_pointer *, queue_pointer *, int);int deleteq(queue_pointer *);

Page 33: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

adjacency list: O(e)adjacency matrix: O(n2)

[0] [1] [2] [3] [4] [5] [6] [7]

0 1 2 3 4 5 6 7

visited:

output:

Breadth First Search : Breadth First Search : ExampleExample

w

out in1 2 3 4 5 60 7

Page 34: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (6/20)Graph Operations (6/20) Connected componentsConnected components

If If GG is an undirected graph, then one can determine w is an undirected graph, then one can determine whether or not it is connected:hether or not it is connected: simply making a call to either simply making a call to either dfsdfs or or bfsbfs then determining if there is any unvisited vertexthen determining if there is any unvisited vertex

adjacency list: O(n+e)adjacency matrix: O(n2)

Page 35: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (7/20)Graph Operations (7/20) Spanning treesSpanning trees

Definition:Definition: A tree A tree TT is said to be a is said to be a spanning treespanning tree of a of a connected graph connected graph GG if if TT is a subgraph of is a subgraph of GG and and TT cont contains all vertices of ains all vertices of GG..

EE((GG): ): TT ( (tree edgestree edges) + ) + NN ( (nontree edgesnontree edges)) TT: set of edges used during search: set of edges used during search NN: set of remaining edges: set of remaining edges

Page 36: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (8/20)Graph Operations (8/20) When graph When graph GG is connected, a depth first or breadth is connected, a depth first or breadth

first search starting at any vertex will visit all vertices first search starting at any vertex will visit all vertices in in GG

We may use DFS or BFS to create a spanning treeWe may use DFS or BFS to create a spanning tree Depth first spanning tree when DFS is usedwhen DFS is used Breadth first spanning tree when BFS is usedwhen BFS is used

Page 37: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (9/20)Graph Operations (9/20)

Properties of spanning trees :Properties of spanning trees : If a nontree edge (If a nontree edge (vv, , ww) is introduced into any spannin) is introduced into any spannin

g tree g tree TT, then a cycle is formed., then a cycle is formed. A spanning tree is a minimal subgraph, A spanning tree is a minimal subgraph, G’G’, of , of GG such such

that that VV((G’G’) = ) = VV((GG) and ) and G’G’ is connected. is connected. We define a minimal subgraph as one with the fewest numbeWe define a minimal subgraph as one with the fewest numbe

r of edger of edge A spanning tree has A spanning tree has nn-1 edges-1 edges

Page 38: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (10/20)Graph Operations (10/20)

Biconnected Graph & Articulation PointsBiconnected Graph & Articulation Points Assumption: Assumption: GG is an undirected, connected graph is an undirected, connected graph

Definition:Definition: A vertex A vertex vv of of GG is an is an articulation pointarticulation point iff t iff the deletion of he deletion of vv, together with the deletion of all edges , together with the deletion of all edges incident to incident to vv, leaves behind a graph that has at least t, leaves behind a graph that has at least two connected components.wo connected components.

Definition:Definition: A A biconnected graphbiconnected graph is a connected graph is a connected graph that has no articulation points.that has no articulation points.

Definition:Definition: A A biconnected componentbiconnected component of a connected of a connected graph graph GG is a maximal biconnected subgraph is a maximal biconnected subgraph HH of of GG.. By maximal, we mean that By maximal, we mean that GG contains no other subgraph that contains no other subgraph that

is both biconnected and properly contains is both biconnected and properly contains HH..

Page 39: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (11/20)Graph Operations (11/20) Examples of Articulation Points (node 1, 3, 5, 7)

1

3 5

6

0 8 9

7 2

4

Connected graph two connected components

one connected graph

1

3 5

6

0 8 9

7

4

2

1

3 5

6

0 8 9

7

4

2

Page 40: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (12/20)Graph Operations (12/20) Biconnected component: Biconnected component:

a maximal connected subgraph Ha maximal connected subgraph H no subgraph that is both biconnected and properly co

ntains H

Page 41: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (13/20)Graph Operations (13/20) Finding the biconnected componentsFinding the biconnected components

By using depth first spanning tree of a connected undireBy using depth first spanning tree of a connected undirected graph cted graph

The The depth first numberdepth first number ((dfn) outside the vertices in the fi) outside the vertices in the figures gives the DFS visit sequencegures gives the DFS visit sequence

If u is an ancestor of v then dfn(u) < dfn(v)

(a) depth first spanning tree

2

4

8 9

3

4 5

7

6

9 8

2

0

1

0

6

7

1 5

3

(b)

result of dfs(3)

1

0

3 0 5 3 5

4 61 6

9 8 9 8

7 7

2

4

2

Page 42: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations Graph Operations (14/20)(14/20)

dfndfn and and lowlow Define Define lowlow((uu): the lowest ): the lowest dfndfn

that we can reach that we can reach from from uu using using a a path of descendantspath of descendants followed followed by by at most one back edgeat most one back edge

low(u) = min{ dfn(u), min{ low(w) | w is a child of u }, min{ dfn(w) | (u, w) is a back edge } }

2

4

8 9

3

4 5

7

6

9 8

2

0

1

0

6

7

1 5

3

nontree edge(back edge)

Page 43: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (15/20)Graph Operations (15/20) Finding an articulation point in a graph:Finding an articulation point in a graph:

Any vertex Any vertex uu is an articulation point is an articulation point iffiff u u is the root of the spanning tree and has two or more childis the root of the spanning tree and has two or more child

renren uu is not the root and has at least one child is not the root and has at least one child ww

such that we such that we cannot reach an ancestor of cannot reach an ancestor of uu using a path that consists of onlyusing a path that consists of only(1) (1) ww(2) (2) descendants of descendants of ww(3)(3) a single back edgea single back edgethus, low(w) dfn(u) 2

4

8 9

3

4 5

7

6

9 8

2

0

1

0

6

7

1 5

3

nontree edge(back edge)

Vertex 0 1 2 3 4 5 6 7 8 9 dfn 4 3 2 0 1 5 6 7 9 8 low 4 0 0 0 0 5 5 5 9 8

Page 44: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (16/20)Graph Operations (16/20) dfndfn and and lowlow values for values for dfsdfs spanning tree with root = 3 spanning tree with root = 3

low(u) = min{ dfn(u), min{ low(w) | w is a child of u }, min{ dfn(w) | (u, w) is a back edge } }

2

4

8 9

3

4 5

7

6

9 8

2

0

1

0

6

7

1 5

3

vertex dfn low child low_child low:dfn arti. point

0 4 4 (4,n,n) null null null:4

1 3 0 (3,4,0) 0 4 4 3

2 2 0 (2,0,n) 1 0 0 < 2

3 0 0 (0,0,n) 4,5 0,5 0,5 0

4 1 0 (1,0,n) 2 0 0 < 1

5 5 5 (5,5,n) 6 5 5 5

6 6 5 (6,5,n) 7 5 5 < 6

7 7 5 (7,8,5) 8,9 9,8 9,8 7

8 9 9 (9,n,n) null null null:9

9 8 8 (8,n,n) null null null:8

Page 45: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (17/20)Graph Operations (17/20) Determining Determining dfndfn and and lowlow

we can easily modify we can easily modify dfsdfs( ) to compute ( ) to compute dfndfn and and lowlow for for each vertex of a connected undirected grapheach vertex of a connected undirected graph

#define MIN2 (x, y) ((x) < (y) ? (x) : (y))short int dfn[MAX_VERTICES];short int low[MAX_VERTICES];int num;

Page 46: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Determining Determining dfndfn and and lowlow (cont’d) (cont’d) we invoke the function with call we invoke the function with call dfnlowdfnlow((xx, -1), where , -1), where

xx is the starting vertex (root) is the starting vertex (root) for the depth first search for the depth first search

v is the parent of u (if any)

u:v:

3

-1

num:

0

dfn:low:

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

0

0

ptr

1

4

3

1

1

2

2

4

2

2

3

1

2

3

3

4

0

1

4

4

5

1

2

0

dfn(u)

w is a child of u

(u, w) is a back edge

w:

42101230

Page 47: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Graph Operations (19/20)Graph Operations (19/20)

Partition the edges of the connected graph into tPartition the edges of the connected graph into their biconnected componentsheir biconnected components In In dfnlowdfnlow, we know that , we know that lowlow[[ww] has been computed fol] has been computed fol

lowing the return from the function call lowing the return from the function call dfnlowdfnlow((ww, , uu)) If If lowlow[[ww] ] dfndfn[[uu],], then we have then we have identified a new biconidentified a new bicon

nected componentnected component We can output all edges in a biconnected component We can output all edges in a biconnected component

if we if we use a stack to save the edgesuse a stack to save the edges when we when we first encfirst encounterounter them them

The function The function biconbicon (Program 6.6) contains the code m (Program 6.6) contains the code modified from odified from dfnlowdfnlow, and the same initialization is used, and the same initialization is used

Page 48: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Find Biconnected componentsFind Biconnected components

ptr

output:

dfn:low:

0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

w is a child of u

(u, w) is a back edge

34

0

0

u:v:

3

-1

num:

0w:

41

4

3

1

1

22

42

2

4

2

2

31

21

1

2

3

3

40

10

0

1

4

4

51

1

20

<1, 0>

23

13

0

2

41

0

4

4

32

0

3

3

-14

<1, 3><2, 1><4, 2><3, 4>

5

35

5

3

5

5

66

56

6

5

6

6

77

67

7

6

7

7

89

79

9

7

8

8

97

7

69

<7, 9>

8

78

8

7

9

9

107

7

68

<7, 8>

65

75

5

6

57

5

back edge or not yet visited5

5

36

<7, 5><6, 7><5, 6>

73

3

-15 <3, 5>

Page 49: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (1/7)(1/7)

IntroductionIntroduction The The costcost of a spanning tree of a weighted, undirected of a spanning tree of a weighted, undirected

graph is the sum of the costs (weights) of the edges igraph is the sum of the costs (weights) of the edges in the spanning tree.n the spanning tree.

A A minimum-cost spanning treeminimum-cost spanning tree is a spanning tree of le is a spanning tree of least cost.ast cost.

Three different algorithms can be used to obtain a minThree different algorithms can be used to obtain a minimum cost spanning tree.imum cost spanning tree. Kruskal’s algorithmKruskal’s algorithm Prim’s algorithmPrim’s algorithm Sollin’s algorithmSollin’s algorithm

All the three use a design strategy called the greedy All the three use a design strategy called the greedy methodmethod

Page 50: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (2/7)(2/7) Greedy StrategyGreedy Strategy

Construct an optimal solution in stagesConstruct an optimal solution in stages At each stage, we make the best decision (selection) At each stage, we make the best decision (selection)

possible at this time.possible at this time. using least-cost criterion for constructing minimum-cost using least-cost criterion for constructing minimum-cost

spanning treesspanning trees

Make sure that the decision will result in a feasible Make sure that the decision will result in a feasible solutionsolution

A feasible solution works within the constraints A feasible solution works within the constraints specified by the problemspecified by the problem

Our solution must satisfy the following constrainsOur solution must satisfy the following constrains Must use only edges within the graph.Must use only edges within the graph. Must use exactly Must use exactly nn - 1 edges. - 1 edges. May not use edges that produce a cycleMay not use edges that produce a cycle

Page 51: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (3/7)(3/7)

Kruskal’s AlgorithmKruskal’s Algorithm Build a minimum cost spanning tree Build a minimum cost spanning tree TT by adding edge by adding edge

s to s to TT one at a time one at a time Select the edges for inclusion in Select the edges for inclusion in TT in non-decreasing in non-decreasing

order of the costorder of the cost An edge is added to An edge is added to TT if it does not form a cycle if it does not form a cycle Since Since GG is connected and has is connected and has nn > 0 vertices, exactly > 0 vertices, exactly

nn-1 edges will be selected-1 edges will be selected Time complexity: Time complexity: OO((ee log log ee)) Theorem 6.1:Theorem 6.1:

Let Let GG be an undirected connected graph. Kruskal’s al be an undirected connected graph. Kruskal’s algorithm generates a minimum cost spanning treegorithm generates a minimum cost spanning tree

Page 52: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (4/7)(4/7)

Kruskal’s Algorithm (cont’d)Kruskal’s Algorithm (cont’d)

0

1

2

34

5 6

28

16

121824

22

25

1014

choose

Page 53: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (5/7)(5/7)

Prim’s AlgorithmPrim’s Algorithm Build a minimum cost spanning tree Build a minimum cost spanning tree TT by adding by adding

edges to edges to TT one at a time. one at a time. At each stage, add a least cost edge to At each stage, add a least cost edge to TT such that such that

the set of selected edges is still a tree.the set of selected edges is still a tree. Repeat the edge addition step until Repeat the edge addition step until TT contains contains nn-1 -1

edges.edges.

Page 54: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (6/7)(6/7) Prim’s Algorithm (cont’d)Prim’s Algorithm (cont’d)

0

1

2

34

5 6

28

16

121824

22

25

1014

Page 55: CS235102 Data Structures Chapter 6 Graphs. Chapter 6 Graphs: Outline  The Graph Abstract Data Type  Graph Representations  Elementary Graph Operations

Minimum Cost Spanning Trees Minimum Cost Spanning Trees (7/7)(7/7)

Sollin’s AlgorithmSollin’s Algorithm Selects several edges for inclusion in Selects several edges for inclusion in TT at each stage. at each stage. At the start of a stage, the selected edges forms a spAt the start of a stage, the selected edges forms a sp

anning forest.anning forest. During a stage During a stage we select a minimum cost edgewe select a minimum cost edge that h that h

as as exactly one vertex in the tree edgeexactly one vertex in the tree edge for for each tree in the forest. each tree in the forest.

Repeat until Repeat until only one tree at the end of only one tree at the end of a stagea stage or or no edges remainno edges remain for selection. for selection. Stage 1: (Stage 1: (00, 5), (, 5), (11, 6), (, 6), (22, 3), (, 3), (33, 2), (, 2), (44, 3),, 3),

((55, 0), (, 0), (66, 1), 1){(0, 5)}, {(1, 6)}, {(2, 3), (4, 3)}{(0, 5)}, {(1, 6)}, {(2, 3), (4, 3)} Stage 2: Stage 2: {(0, 5){(0, 5), (5, 4), (5, 4)}}, , {(1, 6){(1, 6), (1, 2), (1, 2)}},,

{(2, 3), (4, 3){(2, 3), (4, 3), (1, 2), (1, 2)}} Result: {(0, 5), (5, 4), (1, 6), (1, 2), (2, 3), (4, 3)}Result: {(0, 5), (5, 4), (1, 6), (1, 2), (2, 3), (4, 3)}

0

2

34

5 6

28

16

121824

22

25

1014

1