- 125 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Introduction to Data Structure' - gilmore-holly

**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

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

Chapter 6 Graphs

Contents

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

Chapter 6 Graphs

6.1 The Graph Abstract Data Type

p. 263

structureGraph is

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

each edge is a pair of vertices

functions: for all graphGraph, v, v1, and v2Vertices

Graph Create() ::= return an empty graph

Graph 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 FALSE

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

Chapter 6 Graphs

g

c

d

e

A

D

b

a

B

f

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

attaching

Chapter 6 Graphs

V2

1

2

3

6.1.2 Definitions- 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.

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

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

Chapter 6 Graphs

0

0

1

2

1

2

1

3

3

6

5

4

2

G3

Examples for Graph- Examples for Graph

G2

G1

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}

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

Chapter 6 Graphs

0

2

1

3

1

2

Examples for Graphlike Structure

(a)

(b)

Graph with a self edge

Multigraph:

multiple occurrences

of the same edge

Not consider as a graph in this book

Chapter 6 Graphs

1

2

3

Complete Graph

- 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

n(n-1)/2 = 6

Chapter 6 Graphs

1

2

G3

0

1

2

3

Adjacent and Incident

- 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

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>

Chapter 6 Graphs

0

0

0

1

1

1

1

0

2

2

2

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

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?

Chapter 6 Graphs

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

2

3

4

Path

- 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 undirectedor 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

Chapter 6 Graphs

2

3

Simple Path

- 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

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

2

3

4

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.

Chapter 6 Graphs

2

3

不是

strongly

connected

4

strongly

connected

connected connected

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

Chapter 6 Graphs

Connected Component

- Connected Component(Strongly Connected Component):

is a maximal connected subgraph.

eg.

for graph for digraph

1

1

3

2

3

G1

2

4

4

Two connected components of G1

1

1

G2

2

3

2

3

Two strongly connected components of G2

Chapter 6 Graphs

Network

- Network
- A graph with weighted edges is called.
- eg.

台北

90

120

新竹

花蓮

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.

150

100

台中

210

高雄

Chapter 6 Graphs

6.1.3 Graph Representations

- Method 1: Adjacency Matrix
- Method 2: Adjacency Lists
- Method 3: Adjacency Multilists

Chapter 6 Graphs

1

2

3

4

degree =

di

A

1

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

1

0

1

1

0

2

2

3

2

1

0

0

0

1

4

3

1

0

0

1

2

6 / 2 = 3 ... edges

4

0

0

1

0

1

6.1.3.1 Adjacency Matrix- 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.

Chapter 6 Graphs

1

2

0

- not be symmetric

0

0

1

0

1

1

1

1

0

1

2

2

2

0

0

0

0

out-degree =

= dj

1

1

1

in-degree

Adjacency Matrix: Examples- Eg. 2.

Chapter 6 Graphs

Adjacency Matrix: Determine Edges

- 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: Diagonal entries are zero, need not check it)

e = 7 << n2/2 = 32

Chapter 6 Graphs

6.1.3.2 Adjacency Lists

List

Orthogonal list

- Adjacency Lists
- Adjacency matrices for G3
- Adjacency list: edge e 2e nodes.
- Easy random access for any particular vertex

0

G3

1

2

Head Nodes(sequential)

Chapter 6 Graphs

[1]

[2]

[3]

1

2

3

0

2

3

0

0

1

3

1

2

0

1

2

3

Adjacency Lists: Example G1

- An undirected graph with n vertices and e edges

n head nodes and 2e list nodes

Chapter 6 Graphs

Adjacency Lists: Interesting Operations

- 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

G3

1

2

n vertices

# of edges

vs. O(n2) in adjacency matrix

Chapter 6 Graphs

[0]

[1]

[2]

0 NULL

0

1 NULL

1

2

Adjacency Lists: Inverse Adjacency Lists

- Inverse Adjacency List

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

Chapter 6 Graphs

0

tail head column link for head row link for tail

Adjacency Lists: Orthogonal Representation

- Orthogonal representation

Sparse Matrix ???

head node

2

1

0 1

1 2

1

1 0

2

Chapter 6 Graphs

N1

N3

N4

markvertex1 vertex2 path1 path2

N5

N6

0 2 N3 N4

2 3

0 3 N5

1 2 N5 N6

0 1 N2 N4

1 3 N6

[0]

N1

edge(0,1)

[1]

edge(0,2)

N2

[2]

N3

edge(0,3)

[3]

edge(1,2)

N4

0

edge(1,3)

N5

1

2

edge(2,3)

N6

3

6.1.3.3 Adjacency Multilists

- Adjacency Multilists (以 edge 為主)

vertex 0: N1N2N3

vertex 1: N1N4N5

vertex 2: N2N4N6

vertex 3: N3N5N6

Chapter 6 Graphs

markvertex1 vertex2 Link 1 for V1

Link 2 for V2

0 1 N2 N4

2 3

1 3 N6

1 2 N5 N6

0 2 N3 N4

0 3 N5

N2

N1

N3

[0]

N1

edge(0,1)

N4

[1]

N5

N6

edge(0,2)

N2

[2]

N3

edge(0,3)

[3]

edge(1,2)

N4

0

edge(1,3)

N5

1

2

edge(2,3)

N6

3

Adjacency Multilists (cont.)

- Node Structures

vertex 0: N1N2N3

vertex 1: N1N4N5

vertex 2: N2N4N6

vertex 3: N3N5N6

Chapter 6 Graphs

N1

N3

N4

N5

N6

e=6

12 nodes

0

1

2

3

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

e=6 6 nodes

- Adjacency List

0

1

2

3

Chapter 6 Graphs

5

0

1

2

6

G4

3

7

6.2 Elementary Graph Operations

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

Chapter 6 Graphs

Graph Operations: Graph Traversal

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

stack

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

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

1

2

3

Queue

6

4

5

Chapter 6 Graphs

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1

2

0

3

0

4

6

0

5

2

1

1

7

6

5

4

3

1

7

2

7

7

2

7

3

4

5

6

6.2.1 Depth First Search

- Example

Adjacency Lists

Chapter 6 Graphs

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1

2

0

3

0

4

6

0

5

2

1

1

7

6

5

4

3

1

7

2

7

7

2

7

3

4

5

6

Depth First Search: Example

- Example 6.1: Depth first search v0, v1, v3, v7, v4, v5, v2, v6
- Program 6.1, p.273

2

5

4

7

7

stack

3

3

1

1

0

0

Chapter 6 Graphs

1 2 3 4 5 6 7 8

1

2

3

4

5

6

7

8

Depth First Search: Algorithm

- 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

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

Chapter 6 Graphs

Depth First Search: Time Complexity

- Time complexity: Adjacency list
- Time complexity: Adjacency matrix

Adjacency list: O(e)

e: # of edges

e edges 2e nodes

Adjacency matrix: O(n2)

n: # of vertex

Chapter 6 Graphs

[1]

[2]

[3]

[4]

[5]

[6]

[7]

1

2

0

0

3

4

6

0

5

2

1

1

7

6

5

4

3

1

7

7

2

7

2

7

3

4

5

6

6.2.2 Breadth First Search

- Example 6.2: Breadth first search v0, v1,v2, v3, v4, v5, v6, v7
- Program 6:2 , p.275

Chapter 6 Graphs

Breadth First Search: Algorithm

- 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

Q

visited

Chapter 6 Graphs

- eg.

V1

V3

V2

V6

V7

V4

V5

Print:

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

V8

Chapter 6 Graphs

Breadth First Search: Time Complexity

- Time complexity: by using Adjacency Matrix

while

for

n

n

O(n2)

Chapter 6 Graphs

Breadth First Search: Time Complexity

- Time complexity: by using Adjacency List:

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

n

while

for di: degree of (vi)

di

O(e)

Chapter 6 Graphs

Applications of Graph Traversal

- Application 1. Finding components of a graph
- Application 2. Finding a spanning tree of a connected graph
- Application 3. Minimum cost spanning tree

Chapter 6 Graphs

1

5

...

1 1 1 1

2

3

6

7

1

2

3 4 ...

8

4

8

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

6.2.3 Connected Components

Application 1. Finding Components of a Graph

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

Chapter 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 TreeAny 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.

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

Chapter 6 Graphs

- Modify Procedure DFS：同modify Procedure BFS
- e.g.
- Application 1– obtaining circuit equations for an electrical network

BFS: 1,2,3,4

DFS: 1,2,4,3

1

1

1

2

3

2

3

2

3

4

4

4

BF spanning tree

DF spanning tree

Chapter 6 Graphs

0

2

2

1

1

DFS Spanning

6

6

5

5

4

4

3

3

0

7

7

2

1

6

5

4

3

7

DFS Spanning Tree vs. BFS Spanning Tree

BFS Spanning

Chapter 6 Graphs

Download Presentation

Connecting to Server..