Chap06 New

download Chap06 New

of 94

  • date post

    04-Apr-2015
  • Category

    Documents

  • view

    216
  • download

    1

Embed Size (px)

Transcript of Chap06 New

Graphs

Data StructuresChing-Fang Hsu Department of Computer Science and Information Engineering National Cheng Kung University

DefinitionsA graph G consists of two sets.The set of verticesV(G) Finite and nonempty

The set of edgesE(G) Possibly empty

G = (V, E)

An undirected graphThe pair of vertices representing an edge is unordered.2

Definitions (contd.)A directed graphEach edge is represented as a directed pair of vertices. Ex. The pair represents an edge in which u is the tail and v is the head.

The restrictions on graphsNo self loops (aka self edges; p. 268, Fig. 6.3(a)) A graph may not have multiple occurrences of the same edge.Multigraphs (p. 268, Fig. 6.3(b))3

4

Definitions (contd.)A complete graph (p. 267, Fig. 6.2)A graph that has the maximum # of edges The maximum # of edges for a graph with |V| = nUndirected n (n - 1)/2, directed n (n - 1)

Given an edge (u, v)E(G), thenThe vertices u and v are adjacent. The edge (u, v) is incident on vertices u and v.

A subgraph of G (p. 269, Fig. 6.4)A graph G such that V(G) V(G) and E(G) E(G)5

6

7

Definitions (contd.)A path from u to v in a graph GA sequence of vertices, u, i1, i2, , ik, v, such that (u, i1), (i1, i2), , (ik, v) are edges in an undirected graph G.

The length of a pathThe number of edges on the path

A simple pathA path in which all vertices, except possibly the first and the last, are distinct Ex. 0, 1, 3, 2 in p. 267, Fig. 6.2 G18

Definitions (contd.)A cycleA simple path in which the first and the last vertices are the same

Two vertices u and v are connected in an undirected graph G if there is a path in G from u to v.An undirected graph is connected iff , u v, a path from u to v in G.

A connected component ( component) of an undirected graphA maximal connected subgraph Ex. p. 270, Fig. 6.5

9

10

Definitions (contd.)A treeA connected and acyclic graph

A directed graph is strongly connected iff for every pair of distinct vertices u and v in V(G), there is a directed path from u to v and also from v to u. A strongly connected componentA maximal subgraph that is strongly connected

The degree of a vertexThe number of edges incident to that vertex11

Definitions (contd.)For a directed graphin-degree vs. out-degree

Let di be the degree of vertex i in a graph G with |V(G)| = n and |E(G)| = e, then

e = ( di ) / 2i =0

n 1

Directed graphs digraphs The ADT of a graph (p. 271, ADT 6.1)12

13

Graph RepresentationThree most commonly used representationsAdjacency matrices Adjacency lists Adjacency multilists

14

Graph Representation -Adjacency MatriicesA two-dimensional n n array a a[i][j] = 1 iff the edge (i, j) E(G); otherwise, a [i][j] = 0.Ex. p. 272, Fig. 6.7

The main advantageDetermining the degree of a vertex i is a1simple task. nFor an undirected graph: the sum of row i

a[i ][ j ]j =0

For a directed graph: the row sum the out-degree the column sum the in-degree15

16

Graph Representation -Adjacency Matrices (contd.)The main disadvantageAll algorithms that need to examine all edges require (n2) time.n2 - n entries ExamplesHow many edges are there in G? Is G connected?

Not efficient for sparse graphs

17

Graph Representation -Adjacency ListsOne chain for each vertex in G, instead of one row of the adjacency matrix The nodes in chain i represent the vertices that are adjacent from vertex i. For an undirected graph with n vertices and e edges, an array of size n and 2e chain nodes are required.Two fields in a chain node An alternative: sequentially packing the nodes on the adjacency lists

18

Graph Representation -Adjacency Lists (contd.)No pointers; an array node[]node[i]: the starting point of the list for vertex i, 0 i < n node[n]= n + 2e + 1 The vertices adjacent from vertex i: node[i]~ node[i+1]-1

Example: p. 275, Fig. 6.9

For an undirected graphThe degree of any vertex can be determined by counting the # of nodes in its adjacency list. The total # of edges can be determined in (n + e) time.

For a digraphOut-degree the total # of edges (in (n + e))19

20

Graph Representation -Adjacency Lists (contd.)In-degree? More complex!Solution 1. -- Maintaining a second set of lists called inverse adjacency listsEx. p. 275, Fig. 6.10

Solution 2. -- Changing the node structure of the adjacency listsEx. p. 276, Fig. 6.11 Lists in which nodes are shared among several lists

21

22

Graph Representation -Adjacency MultilistsFor each edge, there is exactly one node, but this node is on the adjacency list for each of the two vertices it is incident to.p. 276 Ex. p. 277, Fig. 6.12

23

24

Graph Representation -- Weighted EdgesA graph with weighted edges is called a network. For an adjacency matrixReplace the 1 used to signify an edge with the weight of the edge

For adjacency lists and adjacency multilistsA weight field is needed!

25

Elementary Graph OperationsGiven an undirected graph, G = (V, E), and a vertex vV(G), visit all vertices reachable from v.Depth first searchSimilar to a preorder tree traversal

Breadth first searchSimilar to a level order tree traversal

26

Elementary Graph Operations -Depth First SearchAssume that the start vertex is v.void dfs (int v) { nodePointer w; visited[v] = TRUE; printf (%5d, v); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) dfs (w->vertex); }

Time complexityAdjacency lists: (e) An adjacency matrix: (n2)27

Set current = v

Visit current

Yes

Any unvisited vertex in currents adjacency list?

No

Select an unvisited vertex, w, from currents adjacency list. Push w into a stack

Fail

Pop a vertex u from the stack Success

Set current = w

Termination

Set current = u28

Elementary Graph Operations -Breadth First SearchAssume that the start vertex is v.Visit each of the vertices on vs adjacency list When all the vertices on vs adjacency list have been visited, visit all the unvisited vertices adjacent to the first vertex on vs adjacency list. p. 282, Program 6.2 A dynamically linked queue is exploited.

Time complexityAdjacency lists: (e) An adjacency matrix: (n2)29

30

Elementary Graph Operations -Connected ComponentsBased on graph searches Problem 1: Determine whether or not an undirected graph is connectedCall dfs(0) or bfs(0) and then determine if there are any unvisited vertices Time complexity: (n+e)

Problem 2: List the connected components of a graphp. 283, Program 6.3 Time complexity: (n+e) for adjacency lists while (n2) for an adjacency matrix

31

32

Elementary Graph Operations -Spanning TreesWhat is a spanning tree?Any tree that consists solely of edges in graph G and that includes all the vertices in G A minimal subgraph, G, of G such that V(G) = V(G) and G is connected Given that |V(G)| = n, a spanning tree has n - 1 edges. p. 284, Fig. 6.17

How to create a spanning tree?A depth first spanning tree vs. a breadth first spanning tree p. 285, Fig. 6.18

33

34

Elementary Graph Operations -Biconnected ComponentsAssuming that G is an undirected graph An articulation pointA vertex v such that the deletion of v, together with all edges incident on v, produces a graph G having at least two connected components

A biconnected graphA connected graph that has no articulation points.

A biconnected component H of a connected graph GA maximal biconnected subgraph of G; G contains no other subgraph that is both biconnected and properly contains H. No edge can be in two or more biconnected components of a graph.35

Elementary Graph Operations -Biconnected Components & Articulation Points (contd.)How to find the biconnected components of G?Using any depth first spanning tree of G Example: p. 287, Fig. 6.19(a) and Fig. 6.20Depth first number of v (dfn(v))The sequence in which v is visited during the DFS Generally, if u is an ancestor of v in the depth first spanning tree, then dfn(u) < dfn(v).

A back edge (u, v)A nontree edge iff either u is an ancestor of v or v is an ancestor of u All nontree edges are back edges.

A low value of a vertex u (low(u))The lowest depth dfn that we can reach from u using a path of descendants followed by at 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}}

36

Elementary Graph Operations -Biconnected Components & Articulation Points (contd.)A vertex u is an articulation point iffu is either the root of the spanning tree and has two or more children, or u is not the root and u has a child w such that low(w) >= dfn(u).

Example: p. 288, Fig. 6.21 p. 289, Program 6.4 for dfn and low value determination p. 290, Program 6.6 for finding biconnected components of a graphAssuming that the graph contains at least two vertices

37

38

39

40

41

42

Minimum Cost Spanning TreesThe cost of a spanning tree of a weighted undirected graphThe sum of the costs (weights) of the edges in the spanning tree

A minimum cost spanning tree is a spanning tree of least cost. How to obtain a minimum cost spanning tree?Based on a design strategy called the greedy method43

Minimum Cost Spanning Trees (contd.)Construct an optimal solution in stages At each stage, make a decision that is the best (using some criterion) at this time Make sure that the decision will result in a feasible solution The selection of an item at each stage is typically based on either a lea