Introduction to Data Structure

49
6-1 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type 6.2 Elementary Graph Operations 6.3 Minimum Cost Spanning Trees 6.4 Shortest Paths and Transitive Closure 6.5 Activity Networks

description

Introduction to Data Structure. CHAPTER 6 GRAPHS. 6.1 The Graph Abstract Data Type 6.2 Elementary Graph Operations 6.3 Minimum Cost Spanning Trees 6.4 Shortest Paths and Transitive Closure 6.5 Activity Networks. Contents. Chapter 1 Basic Concepts Chapter 2 Arrays - PowerPoint PPT Presentation

Transcript of Introduction to Data Structure

6-1Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Introduction to Data Structure

CHAPTER 6

GRAPHS

6.1 The Graph Abstract Data Type

6.2 Elementary Graph Operations

6.3 Minimum Cost Spanning Trees

6.4 Shortest Paths and Transitive Closure

6.5 Activity Networks

6-2Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Chapter 1 Basic Concepts

Chapter 2 Arrays

Chapter 3 Stacks and Queues

Chapter 4 Linked Lists

Chapter 5 Trees

Chapter 6 Graph

Chapter 7 Sorting

Chapter 8 Hashing

Chapter 9 Heap Structures

Chapter 10 Search Structures

Contents

6-3Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

6.1 The Graph Abstract Data Type

structure Graph is objects: a nonempty set of vertices and a set of undirected edges, where

each edge is a pair of vertices functions: for all graph Graph, v, v1, and v2 Vertices

Graph Create() ::= return an empty graphGraph InsertVertex(graph, v) ::= return a graph with v inserted. Graph InsertEdge(graph, v1, v2) ::= return a graph with a new edge

(v1, v2).Graph DeleteVertex(graph, v) ::= return a graph with v and all its

incident edges removed.Graph DeleteEdge(graph, v1, v2) ::= return a graph with the edge

(v1, v2) removed.Boolean IsEmpty(graph) ::= if (graph==empty) return TRUE

else return FALSEList Adjacent(graph, v) ::= return a list of all vertices adjacent to v.

p. 263

6-4Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

6.1.1 Introduction 1736, Euler Koenigberg Bridge Problem:

Euler showed: there is a walk starting at any vertex, going through each edge exactly once and terminating at the start vertex iff the degree of each vertex is even

called Eulerian walk. the degree of a vertex: # of edges incident to a vertex

J. R. Newman: the world of Mathematics, 1956, p.573-580

C

A

B

D

g

c d

e

b

f

a

attaching

6-5Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

A Graph, G = (V, E), consists of two sets V and E V: finite non-empty set of vertices E: set of pairs of vertices, edges, e.g.(1,2) or 1,2

Note: V(G): set of vertices of graph G E(G): set of edges of graph G

Undirected Graph – the pair of vertices representing any edge is unordered. Thus, the pairs (V1,V2) and (V2,V1) represent the same edge.

Directed Graph – each edge is represented by a directed pairs V1, V2 Note: V1, V2 and V2, V1 represent two different edges.

6.1.2 Definitions

V1 V2

1

2

3

V(G) = {1, 2, 3}E(G) = {1,2 2,3 3,1}

6-6Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Examples for Graph

0

1 2

3

0

1 2

3 4 5 6

G1 G2

0

1

2

G3

V(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}

Examples for Graph

• Note: Graph G2 is also a tree, Tree is a special case of graph.

6-7Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

(a)

2

1

0

3

(b)

0 2

1

Graph with a self edgeMultigraph:multiple occurrencesof the same edge

Examples for Graphlike Structure

Not consider as a graph in this book

6-8Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

A Complete Graph is a graph that has the maximum number of edges

For undirected graph with n vertices, the maximum number of edges is n(n-1)/2

For directed graph with n vertices, the maximum number of edges is n(n-1)

Example: G1 is a complete graph

Complete Graph

0

1 2

3n(n-1)/2 = 6

6-9Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

If (v0, v1) is an edge in an undirected graph, Adjacent: v0 and v1 are adjacent Incident: The edge (v0, v1) is incident on vertices v0 and v1

If <v0, v1> is an edge in a directed graph Adjacent: v0 is adjacent to v1, and v1 is adjacent from v0 Incident: The edge <v0, v1> is incident on v0 and v1

Adjacent and Incident

0

1

2

G3

0

1 2

3

The vertices adjacent to vertex 2: 0, 1 and 2

The edge incident on vertex 2: (0,2), (1,2) and (2,3)

The vertices adjacent to vertex 1: 0,

The edge incident on vertex 1: <0,1>, <1,0> and <1,2>

6-10Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Subgraph Subgraph

A subgraph of G is a graph G’ →

• V(G’) V(G)

• E(G’) E(G) e.g. some of the subgraphs of G1:

00

1 2 3

1 2

0

1 2

3

(i) (ii) (iii) (iv)G1

0

1 2

3

6-11Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

0

0

1

0

1

2

0

1

2

(i) (ii) (iii) (iv)

0

1

2

G3

Subgraph (cont.)

e.g. some of the subgraphs of G3:

If G is connected with n vertices then its connected subgraph at least has n-1 edges.

• Why?

6-12Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Path: from vp to vq in G is

a sequence of vertices vp, vi1, vi2,...,vin, vp (vp, vi1), (vi1, vi2),..., (vin, vq) E(G) if G is undirected

or vp, vi1, vi1, vi2,..., vin, vq E(G) if G is directed

eg.

Length: The length of a path is the number of edges on it eg. Length of path 1,2,4 is 2

1

2 3

4

- 1,2,4 is a path- 1,2,3 is not a path- 1,2,4,2 is a path

] E(G)(2,3) [

Path

6-13Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Simple Path: is a path in which all vertices except possibly the first and last are distinct.

• e.g.1. 1,2,4 is a simple path, path: 1,2,4,2 is not a simple path.

• e.g.2.

1

2

3

1,2,3,1 also a simple path with length 3.1,2,3,1,2? path, length 4, not simple.1,2,3,1,3 not a path.

1

2 3

4

- 1,2,4 is a path- 1,2,3 is not a path- 1,2,4,2 is a path

Simple Path

6-14Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Cycle Cycle: is a simple path, first and last vertices are same.

• eg. 1,2,3,1.

• Note: Directed cycle, Directed path.

Acyclic graph

1

2

30

1 2

3 4 5 6

G2

tree (acyclic graph)

6-15Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Connected Connected

Two vertices V1 and V2 are Connected:

if in an undirected graph G,

a path in G from V1 to V2 (or from V2 to V1 undirected)∵

Strongly Connected V1, V2 are Strongly Connected:

if in a directed graph (digraph) G’, a path

in G from V1 to V2 and also from V2 to V1

Graph (Strongly) Connected graph connected (graph strongly connected)

if Vi, Vj V(G), a path from Vi to Vj in G

stronglyconnected

不是stronglyconnected

1

2 3

4

connected connected

6-16Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Connected Component Connected Component (Strongly Connected Component):

is a maximal connected subgraph.

eg.

for graph for digraph

1

2

3

4

Two connected components of G1

1

2

1

2

3

Two strongly connected components of G2

1

2 3

4

3G2

G1

6-17Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Degree Degree of Vertex

is the # of edges incident to that vertex eg.

In Direct Graph

If G has n vertices and e edge, di = degree of Vi, then

3 degree of V3 = 3

n

1ii2

1de

in-degreeout-degree

in-degree=1out-degree=2

degree=3

6-18Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Network Network

A graph with weighted edges is called. eg.

台北

花蓮

高雄

台中

新竹

90 120

100

210

150 Diagraph G = (V,E)

Network N = (G,W) is a diagraph G together with a real valued for w: E R.

R is a real number w(u,v) is the weight of edge (arc) (u,v) E.

6-19Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Method 1: Adjacency Matrix

Method 2: Adjacency Lists

Method 3: Adjacency Multilists

6.1.3 Graph Representations

6-20Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Adjacency Matrix Let G = (V, E) with n vertices, n 1.

The adjacency matrix of G is a 2-dimensional n n matrix, A

A(i, j) = 1 iff (vi, vj) ( vi, vj for a diagraph) E(G),

A(i, j) = 0 otherwise.

6.1.3.1 Adjacency Matrix

eg.

1

2 3

4

101004

210013

100012

201101

degree =

di4321

- symmetric- space need n2 bits, but half can be saved

6 / 2 = 3 ... edges

1

0

]][[A n

j

jiA

6-21Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Adjacency Matrix: Examples

0

1

2

111

00002

21011

10100

210- not be symmetric

out-degree =

in-degree

1

0

]][[A n

j

ji= dj

1

0

]][[A n

i

ji

Eg. 2.

6-22Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

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

Examples for Adjacency Matrix

1

0

2

3

4

5

6

7G4

Adjacency Matrix: Examples

6-23Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Questions:

• How many edges e are there in G?

• Determine the # of edges in G?

• Is G connected?

Answers:

• Need check entries of the matrix. check n2 – n time.

spend O(n2) time to determine e Note:

<< n2/2

n

1iid

2

1e

(n: Diagonal entries are zero, need not check it)

Adjacency Matrix: Determine Edges

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

e = 7 << n2/2 = 32

6-24Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Adjacency Lists

Adjacency matrices for G3

Adjacency list: edge e 2e nodes. Easy random access for any particular vertex

ListOrthogonal list

0

1

2

0 1 0

1 0 1

0 0 0

V0

V1

V2 0

1 0

0 2 0

V0

V1

V2 0

1 0

2 0 0

Head Nodes(sequential)

6.1.3.2 Adjacency Lists

G3

6-25Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

[0]

[1]

[2]

[3]

1 2 3

0 2 3

0 1 3

0 1 2

An undirected graph with n vertices and e edges

n head nodes and 2e list nodes

0

1 2

3

Adjacency Lists: Example G1

6-26Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1 2

0 3

0 3

1 2

5

4 6

5 7

6

1

0

2

3

4

5

6

7G4

Adjacency Lists: Example G4

6-27Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Number of nodes in a graph:

= the number of nodes in adjacency list

Number of edges in a graph

= determined in O(n+e)

Out-degree of a vertex in a directed graph

= the number of nodes in its adjacency list

In-degree of a vertex in a directed graph:

??? traverse the whole data structure

0

1

2

V0

V1

V2 0

1 0

2 0 0

G3

Adjacency Lists: Interesting Operations

n vertices# of edges

vs. O(n2) in adjacency matrix

6-28Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

[0]

[1]

[2]

1 NULL

0 NULL

1 NULL

determine in-degree of a vertex in a fast way.

0

1

2

Inverse Adjacency List

Adjacency Lists: Inverse Adjacency Lists

6-29Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

tail head column link for head row link for tail

Adjacency Lists: Orthogonal Representation

Orthogonal representation

0

1

0

1

0

0

0

1

0

Sparse Matrix ???

0 1 2

0

1

2

0 1

1 0 1 2

head node

6-30Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

6.1.3.3 Adjacency Multilists Adjacency Multilists ( 以 edge 為主 )

mark vertex1 vertex2 path1 path2

0

1 2

3

N1 N2N3

N4N5 N6

vertex 0: N1N2N3vertex 1: N1N4N5vertex 2: N2N4N6vertex 3: N3N5N6

[0]

[1]

[2]

[3]

0 1 N2 N4

0 2 N3 N4

0 3 N5

1 2 N5 N6

1 3 N6

2 3

N1

N2

N3

N4

N5

N6

edge(0,1)

edge(0,2)

edge(0,3)

edge(1,2)

edge(1,3)

edge(2,3)

6-31Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Adjacency Multilists (cont.)

0

1 2

3

N1 N2N3

N4N5 N6

vertex 0: N1N2N3vertex 1: N1N4N5vertex 2: N2N4N6vertex 3: N3N5N6

mark vertex1 vertex2 Link 1 for V1 Link 2 for V2

Node Structures

[0]

[1]

[2]

[3]

0 1 N2 N4

0 2 N3 N4

0 3 N5

1 2 N5 N6

1 3 N6

2 3

N1

N2

N3

N4

N5

N6

edge(0,1)

edge(0,2)

edge(0,3)

edge(1,2)

edge(1,3)

edge(2,3)

6-32Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Adjacency MultiLists vs. Adjacency List Adjacency Multilists ( 以 edge 為主 )

e=6 6 nodes

Adjacency List

0

1 2

3

e=6 12 nodes

0

1 2

3

N1 N2N3

N4N5 N6

6-33Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

TraversalGiven G = (V, E) and vertex v, find or visit all w V, such that w connects v.

Method 1: Depth First Search (DFS) preorder tree traversal

Method 2: Breadth First Search (BFS) level order tree traversal

Connected Components (Application 1 of Graph traversal) Spanning Trees (Application 2 of Graph traversal) Minimum Cost Spanning Tree (Application 3 of Graph

traversal) …

6.2 Elementary Graph Operations

1

0

2

3

4

5

6

7G4

6-34Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Graph TraversalGiven: an undirected graph G = (V, E), a vertex v V(G)Interested in: visiting all vertices connected to v. (reachable)Approach: 1. Depth First Search (DFS). 2. Breadth First Search (BFS).

1

2 3

4 5 6

DFS: 1,2,4,5,3,6BFS: 1,2,3,4,5,6

4

2

1

stack

1 2 3

Queue

Graph Operations: Graph Traversal

6-35Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

0

1 2

3 4 5 6

7

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1 2

0 3

0 5

1 7

2 7

3

4

6

1 7

2 7

4 5 6

Example

6.2.1 Depth First Search

Adjacency Lists

6-36Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Example 6.1: Depth first search v0, v1, v3, v7, v4, v5, v2, v6

Program 6.1, p.273

0

1 2

3 4 5 6

7

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1 2

0 3

0 5

1 7

2 7

3

4

6

1 7

2 7

4 5 6

Depth First Search: Example

0

1

3

7

stack

0

1

3

7

5

2 4

6-37Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Depth First SearchProcedure DFS(v) (* Array VISITED(n) 0 initially and VISITED(i) 1, if has visited*) VISITED(v) 1 print(v) for each vertex w adjacent to v do if VISITED(w) = 0 then call DFS(w) end end DFS

Depth First Search: Algorithm

VISITED

1 2 3 4 5 6 7 8

1

2 3

4 5 6 7

8

Output DFS: 1,2,4,8,5,6,3,7

V1

V2

V3

V2

V4

V5

V4

V8

V8

V5

V6

V7

V5 V6

V3

V3

V7

V7

6-38Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Time complexity: Adjacency list

Time complexity: Adjacency matrix1 2 8

1

2

3

8

1

2

3

4

5

6

7

8

2 3 0

1 4 5 0

e edges 2e nodes

1 6 7 0

Depth First Search: Time Complexity

Adjacency matrix: O(n2) n: # of vertex

Adjacency list: O(e) e: # of edges

6-39Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Example 6.2: Breadth first search v0, v1, v2, v3, v4, v5, v6, v7

Program 6:2 , p.275

0

1 2

3 4 5 6

7

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1 2

0 3

0 5

1 7

2 7

3

4

6

1 7

2 7

4 5 6

6.2.2 Breadth First Search

6-40Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Breadth First Search

procedure BFS(v) VISITED (v) 1 print (v) initialize Q with v while Q not = do call DELETE Q(v,Q) for all vertices w adjacent to v do if VISITED (w)=0 then [ call ADDQ(w,Q); VISITED(w) 1, print(w)] end end end BFS 1 2 3 4 5 6 7 8

1

visited

V …

Breadth First Search: Algorithm

Q

6-41Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

eg.

V1

V2V3

V4 V5V6 V7

V8

Q

V1 V2 V3 V4 V5 V6 V7 V8

Print: V1 , V2 , V3 , V4 , V5 , V6 , V7 , V8 .

Breadth First Search: Example

6-42Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Time complexity: by using Adjacency Matrix

1 2 3 4 5 6 7 8

1 0 1 1 0 0 0 0 0

2 1 0 0 1 1 0 0 0

3 0

4 0

5 0

6 0

7 0

8 0

while

for n

n

Breadth First Search: Time Complexity

O(n2)

6-43Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Time complexity: by using Adjacency List:

Total: d1 + d2 +...+ dn = O(e) where di = degree (vi)

while

for di: degree of (vi)di

n

Breadth First Search: Time Complexity

O(e)

6-44Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

• Application 1. Finding components of a graph

• Application 2. Finding a spanning tree of a connected

graph

• Application 3. Minimum cost spanning tree

Applications of Graph Traversal

6-45Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Procedure COMP(G)(* determine the connected components of G *) for i 1 to n do VISITED(i) 0 for i 1 to n do if VISITED(i) = 0 then call DFS(i); print(“||”) end end COMP

Determine connected graph Invoke DFS or BFS

Application 1. Finding Components of a Graph

6.2.3 Connected Components

1

2 3

4

5

6 7

8

DFS: 1 2 4 3 || 5 6 7 8 || BFS: 1 2 3 4 || 5 6 7 8 ||

...

8 3 4 ...21

VISITED

1 1 1 1

6-46Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Application 2. Finding a Spanning Tree of a Connected Graph

Def. Application 2.1– obtaining circuit equations for an electrical network Application 2.2 – minimum cost spanning tree

Def. Spanning Tree

Any tree consisting only edges in G and including all vertices in G is called.

• i.e. minimal subgraph G’ of G, such that V(G’) =V(G) and G’ is connected.

6.2.4 Spanning Trees

6-47Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

eg.

How to find it?

• Modify BFS Procedure BFS(v)

If VISITTED(w)=0 then If VISITTED(w)=0 then

1.call ADDQ(w,Q) 1.

2.VISITTED(w)←1 2.

3.print(w) 3.

4.T = T {(∪ v, w)}

...(How many?)

Ans : 16 (EX.)

Spanning Trees (cont.)

6-48Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

Modify Procedure DFS:同 modify Procedure BFS e.g.

Application 1– obtaining circuit equations for an electrical network

1

2 3

4

1

2 3

4

BFS: 1,2,3,4

1

2 3

4

DFS: 1,2,4,3

BF spanning tree DF spanning tree

Spanning Trees (cont.)

6-49Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Li

nChapter 6 Graphs

BFS Spanning

DFS Spanning Tree vs. BFS Spanning Tree

0

1 2

3 4 5 6

7DFS Spanning

0

1 2

3 4 5 6

7

0

1 2

3 4 5 6

7