Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.

Similar presentations


Presentation on theme: "Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations."— Presentation transcript:

1 Lecture #13

2 Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.

3 The Graph ADT Definitions – A graph G consists of two sets a finite, nonempty set of nodes V(G) a finite, possible empty set of edges E(G) – G(V,E) represents a graph – An undirected graph is one in which the pair of nodes in an edge is unordered, (v 0, v 1 ) = (v 1,v 0 ) – A directed graph is one in which each edge is a directed pair of nodes, != tail head

4 The Graph ADT Examples for Graph – complete undirected graph: n(n-1)/2 edges – complete directed graph: n(n-1) edges V(G 1 )={0,1,2,3} E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G 2 )={0,1,2,3,4,5,6} E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G 3 )={0,1,2} E(G 3 )={,, } 0 12 3 G1G1 complete graph 0 12 3 4 5 6 G2G2 0 1 2 G3G3 incomplete graph 0 12 3

5 The Graph ADT Restrictions on graphs – A graph may not have an edge from a node, i, back to itself. Such edges are known as self loops – A graph may not have multiple occurrences of the same edge. If we remove this restriction, we obtain a data referred to as a multigraph feedback loops multigraph

6 The Graph ADT Adjacent and Incident If (v 0, v 1 ) is an edge in an undirected graph, – v 0 and v 1 are adjacent – The edge (v 0, v 1 ) is incident on nodes v 0 and v 1 If is an edge in a directed graph – v 0 is adjacent to v 1, and v 1 is adjacent from v 0 – The edge is incident on v 0 and v 1 V0V0 V1V1 V0V0 V1V1

7 The Graph ADT A subgraph of G is a graph G’ such that V(G’)  V(G) and E(G’)  E(G). 0 12 3 G1G1 0 1 2 G3G3

8 The Graph ADT Path – A path from node v p to node v q in a graph G, is a sequence of nodes, v p, v i 1, v i 2,..., v i n, v q, such that (v p, v i 1 ), (v i 1, v i 2 ),..., (v i n, v q ) are edges in an undirected graph. A path such as (0, 2), (2, 1), (1, 3) is also written as 0, 2, 1, 3 – The length of a path is the number of edges on it 0 12 3 0 12 3 0 12 3

9 The Graph ADT Simple path and cycle – simple path (simple directed path): a path in which all nodes, except possibly the first and the last, are distinct. – A cycle is a simple path in which the first and the last nodes are the same. 0 12 3

10 The Graph ADT Connected graph – In an undirected graph G, two nodes, v 0 and v 1, are connected if there is a path in G from v 0 to v 1 – An undirected graph is connected if, for every pair of distinct nodes v i, v j, there is a path from v i to v j Connected component – A connected component of an undirected graph is a maximal connected subgraph. – A tree is a graph that is connected and acyclic (i.e, has no cycle).

11 The Graph ADT Strongly Connected Component – A directed graph is strongly connected if there is a directed path from v i to v j and also from v j to v i – A strongly connected component is a maximal subgraph that is strongly connected 2 0 1 0 1 2 G3G3 not strongly connected strongly connected component (maximal strongly connected subgraph)

12 The Graph ADT Degree – The degree of a node is the number of edges incident to that node. For directed graph – in-degree (v) : the number of edges that have v as the head – out-degree (v) : the number of edges that have v as the tail If d i is the degree of a node i in a graph G with n nodes and e edges, the number of edges is

13 The Graph ADT Degree (cont’d) We shall refer to a directed graph as a digraph. When we us the term graph, we assume that it is an undirected graph undirected graph degree in-degree & out-degree 0 1 2 G3G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 directed graph 3 0 12 3 3 3 3 G1G1 0 12 3 4 5 6 2 33 111 G2G2 1

14 The Graph ADT Abstract Data Type Graph struct Graph is object: a nonempty set of nodes and a set of undirected edges, where each edge is a pair of nodes. functions: for all graph  Graph, v, v 1 and v 2  Nodes Graph Create( ) ::= return an empty graph. Graph InsertNode( Graph, v) ::= return a graph with v inserted v has no incident edges. Graph InsertEdge(Graph, v 1, v 2 ) ::= return a graph with a new edges between v 1 and v 2. Graph DeleteNode(Graph, v) ::= return a graph in which v and all edges incident to it are removed. Graph DeleteEdge(Graph, v 1, v 2 ) ::= return a graph in which the edge (v 1, v 2 ) is removed. Leave the incident nodes in the graph. bool IsEmpty(graph) ::= if(graph==empty graph) return True else return False. List Adjacent(graph) ::= return a list of all nodes that are adjacent to v.

15 2 Graph Representations Adjacency Matrix Adjacency Lists Adjacency Multilists

16 Graph Representations Adjacency Matrix – Let G = (V,E) be a graph with n nodes. – The adjacency matrix of G is a two-dimensional n x n array, say adj_mat – If the edge (v i, v j ) is(not) in E(G), adj_mat[i][j]=1(0) – The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric G1G1 G3G3 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7

17 Graph Representations Meritx of Adjacency Matrix – For an undirected graph, the degree of any node, i, is its row sum: – For a directed graph, the row sum is the out-degree, while the column sum is the in-degree. – The complexity of checking edge number or examining if G is connect G is undirected: O(n 2 /2) G is directed: O(n 2 )

18 Graph Representations Adjacency lists – There is one list for each node in G. The nodes in list i represent the nodes that are adjacent from node i – For an undirected graph with n nodes and e edges, this representation requires n head nodes and 2e list nodes – C declarations for adjacency lists #define MAX_NODES 50; /* maximum number of nodes*/ struct node { int node; node * link; } node graph(MAX_NODES); int n=0; /* nodes currently in use */

19 01230123 012012 0123456701234567 1 23 023 013 0 12 G1G1 1 0 2 G3G3 1 2 0 3 0 3 12 5 4 6 57 6 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes Example

20 Graph Representations Adjacency Multilists – Lists in which nodes may be shared among several lists. (an edge is shared by two different paths) – There is exactly one node for each edge. – This node is on the adjacency list for each of the two nodes it is incident to struct edge { short int market; int node1; int node2 edge * path1; edge * path2; } edge graph(MAX_NODES); Marked node1 node2 path1 path2

21 Graph Representations Example for Adjacency Multlists 0 12 3 The lists are: node 0: N1  N2  N3 node 1: N1  N4  N5 node 1: N1  N4  N5 node 2: N2  N4  N6 node 2: N2  N4  N6 node 3: N3  N5  N6 node 3: N3  N5  N6

22 Graph Representations Weighted edges – The edges of a graph have weights assigned to them. – These weights may represent as the distance from one node to another cost of going from one node to an adjacent node. – adjacency matrix: adj_mat[i][j] would keep the weights. – adjacency lists: add a weight field to the node structure. – A graph with weighted edges is called a network

23 3 Graph Operations Traversal Given G=(V,E) and node v, find all w  V, such that w connects v – Depth First Search (DFS): preorder traversal – Breadth First Search (BFS): level order traversal Spanning Trees Biconnected Components

24 Graph Traversal Problem: Search for a certain node or traverse all nodes in the graph Depth First Search – Once a possible path is found, continue the search until the end of the path Breadth First Search – Start several paths at a time, and advance in each one step at a time

25 Graph Traversal Example for traversal (using Adjacency List representation of Graph) depth first search (DFS): v 0, v 1, v 3, v 7, v 4, v 5, v 2, v 6 breadth first search (BFS): v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7

26 Depth-First Search

27 void dfe(int v) { /* depth first search of a graph begin*/ node w; visited[v]= TRUE; cout<<v; for (w=graph(v); w; w= w->link) if (!visited[w->node]) if (!visited[w->node])dfs(w->node);} Depth First Search short int visited[MAX_NODES]; Data structure adjacency list: O(e) adjacency matrix: O(n 2 ) w [0][1][2][3][4][5][6][7] output:01374526 visited:

28 Breadth First Search – It needs a queue to implement breadth-first search – void bfs(int v): breadth first traversal of a graph starting with node v the global array visited is initialized to 0 the queue operations are similar to those described in Chapter 18 struct queue { int node; queue * link; }; void addq(queue *, queue *, int); int deleteq(queue *);

29 29 BFS - A Graphical Representation d) c) b)a)a)

30 More BFS

31 node w; queue front, rear; front=rear =NULL; /* initialize queue cout<<v; visited[v] = TURE; addq(&front, &rear, v); while (fornt) { v= deleteq(&front); v= deleteq(&front); for(w = graph[v]; w; w=w->link) for(w = graph[v]; w; w=w->link) if (!visited[w->node]) { if (!visited[w->node]) { cout node; cout node; addq(&front, &rear, w->node); addq(&front, &rear, w->node); node[w->node]=TRUE; node[w->node]=TRUE; } } adjacency list: O(v+e) adjacency matrix: O(n 2 ) [0][1][2][3][4][5][6][7] 01234567 visited: output: Breadth First Search : Example w outin 12345607

32 Applications: Finding a Path Find path from source node s to destination node d Use graph search starting at s and terminating as soon as we reach d – Need to remember edges traversed Use depth – first search ? Use breath – first search?

33 DFS vs. BFS E F G B C D A start destination A DFS on A A DFS on B B A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on G G found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process

34 DFS vs. BFS E F G B C D A start destination BFS Process A Initial call to BFS on A Add A to queue B Dequeue A Add B frontrearfrontrear C Dequeue B Add C, D frontrear DD Dequeue C Nothing to add frontrear G Dequeue D Add G frontrear found destination - done! Path must be stored separately

35 Spanning trees Connected components – If G is an undirected graph, then one can determine whether or not it is connected: simply making a call to either dfs or bfs then determining if there is any unvisited node adjacency list: O(n+e) adjacency matrix: O(n 2 ) Program: Connected components void connected ( ) { /* determine the connected components of a graph*/ int i; for (i=0; i<n ; i++) if( !visited[i]) { if( !visited[i]) { dfs(i); dfs(i); cout<<endl; cout<<endl; } }

36 Spanning trees – Definition: A tree T is said to be a spanning tree of a connected graph G if T is a subgraph of G and T contains all nodes of G. – E(G): T (tree edges) + N (nontree edges) T: set of edges used during search N: set of remaining edges

37 Spanning trees When graph G is connected, a depth first or breadth first search starting at any node will visit all nodes in G We may use DFS or BFS to create a spanning tree – Depth first spanning tree when DFS is used – Breadth first spanning tree when BFS is used

38 Spanning trees Properties of spanning trees : – If a nontree edge (v, w) is introduced into any spanning tree T, then a cycle is formed. – A spanning tree is a minimal subgraph, G’, of G such that V(G’) = V(G) and G’ is connected. We define a minimal subgraph as one with the fewest number of edge A spanning tree has n-1 edges

39 Biconnected Graph Biconnected Graph & Articulation Points: Assumption: G is an undirected, connected graph an articulation point – Definition: A node v of G is an articulation point if the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components. biconnected graph – Definition: A biconnected graph is a connected graph that has no articulation points. biconnected component – Definition: A biconnected component of a connected graph G is a maximal biconnected subgraph H of G. By maximal, we mean that G contains no other subgraph that is both biconnected and properly contains H.

40 Biconnected Graph 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 7 4 2

41 Biconnected Graph Biconnected component: a maximal connected subgraph H – no subgraph that is both biconnected and properly contains H


Download ppt "Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations."

Similar presentations


Ads by Google