1.54k likes | 2.09k Vues
Chapter #6: GRAPHS Fundamentals of Data Structures in C Horowitz, Sahni and Anderson-Freed Computer Science Press July, 1997. C. d. g. c. C. g. c. d. D. e. A. e. A. D. Kneiphof. a. b. f. B. f. a. b. B. The Graph Abstract Data Type. Introduction
E N D
Chapter #6: GRAPHS Fundamentals of Data Structures in C Horowitz, Sahni and Anderson-Freed Computer Science Press July, 1997
C d g c C g c d D e A e A D Kneiphof a b f B f a b B The Graph Abstract Data Type • Introduction • the bridges of Koenigsberg (a) (b)
The Graph Abstract Data Type • Definition • a graph G = (V,E) where • V(G): set of vertices • - finite and nonempty • E(G): set of edges • - finite and possibly empty • undirected graph • - unordered: (vi,vj) = (vj,vi) • directed graph • - ordered: <vi,vj> ¹ <vj,vi>
0 0 0 1 2 1 1 2 3 3 4 5 6 2 The Graph Abstract Data Type • three sample graphs • 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>} G1 G2 G3
0 0 2 1 3 1 2 The Graph Abstract Data Type • restrictions on graphs • 1)no edge from a vertex, i, back to • itself (no self loop) • - not allowed (vi,vi) or <vi,vi> • 2)no multiple occurrences of the • same edge ( multigraph) • examples of graph with feedback • loops and a multigraph (a) (b)
The Graph Abstract Data Type • complete graph • - the maximum number of edges • - undirected graph with n vertices • max number of edges = n(n-1)/2 • - directed graph with n vertices • max number of edges = n(n-1)
The Graph Abstract Data Type • adjacent • - vi and vj are adjacent • if (vi,vj) E(G) • adjacent to(from) for digraphs • - <v0,v1>: a directed edge • - vertex v0 is adjacentto vertex v1 • - vertex v1 is adjacentfrom vertex v0
The Graph Abstract Data Type • incident • - an edge e = (vi,vj) is incident on • vertices vi and vj • - an edge <vi,vj> is incident on • vi and vj
0 1 2 0 0 1 2 3 1 2 3 (i) (ii) (iii) (iv) The Graph Abstract Data Type • subgraph G’ of G • - V(G’) Í V(G) and • - E(G’) Í E(G) some of the subgraphs of G1
0 0 0 0 1 1 1 2 2 The Graph Abstract Data Type (i) (ii) (iii) (iv) some of the subgraphs of G3
The Graph Abstract Data Type • path (from vertex vp to vertex vq) • - a sequence of vertices, • vp, vi1, vi2, ···, vin, vq such that • (vp,vi1),(vi1,vi2),···,(vin,vq) are • edges in an undirected graph or • <vp,vi1>,<vi1,vi2>,···,<vin,vq> are • edges in a directed graph • length of path • - number of edges on the path
The Graph Abstract Data Type • simple path • - a path in which all vertices, • except possibly the first and the • last, are distinct • cycle • - a path in which the first and last • vertices are the same • - simple cycle • for directed graph • - add the prefix “directed” to the • terms cycle and path • - simple directed path • - directed cycle • - simple directed cycle
The Graph Abstract Data Type • connected • - vertex v0 and v1 is connected, if • there is a path from v0 to v1 in an • undirected graph G • - an undirected graph is connected • if, for every pair of vertices vi • and vj, there is a path from vi to • vj
4 0 5 1 2 6 3 7 The Graph Abstract Data Type • connected component • (of an undirected graph) • - maximal connected subgraph • a graph with two connected components H1 H2 G4
1 4 2 3 The Graph Abstract Data Type • strongly connected • (in a directed graph) • - for every pair of vertices vi, vj • in V(G) there is a directed path • from vi to vj and also from vj to vi • strongly connected directed graph
0 2 1 The Graph Abstract Data Type • strongly connected component • - maximal subgraph that is strongly • connected • strongly connected components of G3
The Graph Abstract Data Type • degree (of a vertex) • - number of edges incident to that • vertex • in-degree (of a vertex v) • - number of edges that have v as the • head (for directed graphs) • out-degree (of a vertex v) • - number of edges that have v as the • tail (for directed graphs)
The Graph Abstract Data Type • special types of graphs • tree • - an acyclic connected graph • bipartite garph • planar graph • complete graph
Graph Representations • Adjacency matrix • - G = (V,E) with |V| = n(³1) • - two-dimensional n n array, • say adj_mat[][] • - adj_mat[i][j] = • “1” if (vi,vj) is adjacent • “0” otherwise • - space complexity: S(n) = n2 • - symmetric for undirected graphs • - asymmetric for directed graphs
Graph Representations • adjacency matrices for G1, G3, and G4 G1 G3 G4
vertex link Graph Representations • Adjacency lists • - replace n rows of adjacency matirx • with n linked lists • - every vertex i in G has one list • #define MAX_VERTICES 50 • typedef struct node *node_ptr; • typdef struct node { • int vertex; • node_ptr link; • }; • node_ptr graph[MAX_VERTICES]; • int n = 0; /* vertices currently in use */
headnode vertex link 0 1 2 3 1 0 2 3 2 0 1 3 3 0 1 2 0 1 1 0 2 2 Graph Representations G1 G3
0 1 2 1 0 3 2 0 3 3 1 2 4 5 5 4 6 6 5 7 7 6 Graph Representations • adjacency lists for G1, G3, and G4 G4
0 1 1 0 2 1 Graph Representations • Inverse adjacency lists • - useful for finding in-degree of a • vertex in digraphs • - contain one list for each vertex • - each list contains a node for each • vertex adjacent to the vertex that • the list represents • inverse adjacency list for G3
tail head column link for head row link for tail 0 1 2 0 Graph Representations • Orthogonal representation • - change the node structure of the • adjacency lists • orthogonal representation for graph G3 headnodes (shown twice) 0 1 0 1 1 1 2 2
headnode vertex link 0 3 1 2 1 2 0 3 2 3 0 1 3 2 1 0 Graph Representations • vertices may appear in any order • alternate order adjacency list for G1
Graph Representations • adjacency multilists • - for undirected graph • - lists in which nodes are shared • among several lists • - exactly one node for each edges • - this node is on the adjacency list • for each of the two vertices it is • incident to
marked vertex1 vertex2 path1 path2 Graph Representations • typedef struct edge *edge_ptr; • typedef struct edge { • short int marked; • int vertex1; • int vertex2; • edge_ptr path1; • edge_ptr path2; • }; • edge_ptr graph[MAX_VERTICES]; • node structure for adjacency • multilists
head nodes 0 N1 0 1 N2 N4 edge(0,1) 1 N2 0 2 N3 N4 edge(0,2) 2 3 N3 0 3 NULL N5 edge(0,3) N4 1 2 N5 N6 edge(1,2) N5 1 3 NULL N6 edge(1,3) N6 2 3 NULL NULL edge(2,3) The lists are: vertex 0:N1 N2 N3 vertex 1:N1 N4 N5 vertex 2:N2 N4 N6 vertex 3:N3 N5 N6 Graph Representations • adjacency multilists for G1
Graph Representations • weighted edges • - assign weights to edges of a graph • 1)distance from one vertex to • another, or • 2)cost of going from one vertex to • an adjacent vertex • - modify representation to signify • an edge with the weight of the edge • for adj matrix : weight instead of 1 • for adj list : weight field • network • - a graph with weighted edges
Elementary Graph Operations • graph traversals • - visit every vertex in a graph • - what order? • DFS(Depth First Search) • - similar to a preorder tree • traversal • BFS(Breath First Search) • - similar to a level-order tree • traversal
v0 v1 v2 v3 v4 v5 v6 v7 Elementary Graph Operations (a)
0 1 2 1 0 3 4 2 0 5 6 3 1 7 4 1 7 5 2 7 6 2 7 7 3 4 5 6 Elementary Graph Operations • graph G and its adjacency lists (b)
Elementary Graph Operations • depth first search • easy to implement recursively • - stack • a global array visited[MAX_VERTICES] • - initialized to FALSE • - change visited[i] to TRUE when a • vertex i is visited • #define FALSE 0 • #define TRUE 1 • short int visited[MAX_VERTICES];
visited: 0 v0 1 2 3 v1 v2 4 5 v3 v4 v5 v6 6 7 v7 v4 v7 v7 v7 v3 v3 v3 v1 v1 v1 v1 v0 v0 v0 v0 v0 Elementary Graph Operations Ex)
Elementary Graph Operations • void dfs(int v) { • /* depth first search of a graph beginning with • vertex v */ • node_ptr w; • visited[v] = TRUE; • printf(“%5d”, v); • for (w = graph[v]; w; w = w->link) • if (!visited[w->vertex]) • dfs(w->vertex); • } • depth first search • - time complexity for adj list • representation: O(e) • - time complexity for adj matrix • representation: O(n2)
Elementary Graph Operations • breadth first search • use a dynamically linked queue • - each queue node contains vertex • and link fields • typedef struct queue *queue_ptr; • typedef struct queue { • int vertex; • queue_ptr link; • }; • void insert(queue_ptr *, queue_ptr *, int); • void delete(queue_ptr *);
v0 v1 v2 v1 v2 v2 v3 v4 v3 v4 v5 v6 front v3 v4 v5 v4 v5 v6 v7 v5 v6 v7 v6 v7 v6 v7 v7 Elementary Graph Operations
Elementary Graph Operations • void bfs(int v) { • node_ptr w; queue_ptr front, rear; • front = rear = NULL;/* initialize queue */ • printf(“%5d”, v); • visited[v] = TRUE; • insert(&front, &rear, v); • while (front) { • v = delete(&front); • for (w = graph[v]; w; w = w->link) • if (!visited[w->vertex]) { • printf(“%5d”, w->vertex); • add(&front, &rear, w->vertex); • visited[w->vertex] = TRUE; • } • } • } • breath first search of a graph
Elementary Graph Operations • time complexity for bfs() • - time complexity for adj list • representation: O(e) • - time complexity for adj matrix • representation: O(n2)
Elementary Graph Operations • connected components • determine whether or not an • undirected graph is connected • - simply calling dfs(0) or bfs(0) • and then determine if there are • unvisited vertices • list the connected components of a • graph • - make repeated calls to either • dfs(v) or bfs(v) where v is an • unvisited vertex
Elementary Graph Operations • void connected(void) { • /* determine the connected components of a • graph */ • int i; • for (i = 0; i < n; i++) • if (!visited[i]) { • dfs(i); • printf(“\n”); • } • } • connected components • time complexity: O(n+e) • - total time by dfs: O(e) • - for loop: O(n)
Elementary Graph Operations • spanning trees • when graph G is connected dfs or bfs • implicitly partitions the edges • in G into two sets: • T(for tree edges): set of edges used • or traversed during the search • N(for nontree edges): set of • remaining edges • - edges in T form a tree that includes • all vertices of G • Def) A spanning tree is any tree that • consists solely of edges in G and • that include all the vertices in G
Elementary Graph Operations • a complete graph and three of its • spanning trees
Elementary Graph Operations • depth first spanning tree • - use dfs to create a spanning tree • breadth first spanning tree • - use bfs to create a spanning tree
v0 v0 v1 v2 v1 v2 v3 v4 v5 v6 v3 v4 v5 v6 v7 v7 Elementary Graph Operations • dfs and bfs spanning trees (a) dfs(0) spanning tree (b) bfs(0) spanning tree
Elementary Graph Operations • properties of spanning trees • 1) if we add a nontree edge into a • spanning tree cycle • 2) spanning tree is a minimal subgraph • G’, of G such that V(G’) = V(G) and • G’ is connected • 3) |E(G’)| = n - 1 where |V(G)| = n • minimum cost spanning trees
Elementary Graph Operations • biconnected components and • articulation points(cut-points) • articulation point • - a vertex v of G • - deletion of v, together with all • edges incident on v, produce a • graph, G’, that has at least two • connected components • biconnected graph • - a connected graph that has no • articulation points
v0 v1 v2 v3 v4 v5 v6 v7 Elementary Graph Operations Example of biconnected graph
0 8 9 1 7 2 3 5 4 6 Elementary Graph Operations • a connected graph which is not • biconnected • articulation points are 1,3,5,7