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

Slides:



Advertisements
Similar presentations
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Advertisements

CHAPTER 6 GRAPHS All the programs in this file are selected from
Review Binary Search Trees Operations on Binary Search Tree
Graph Theory.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CS212: Data Structures and Algorithms
Data Structures 13th Week
Csc 2720 Instructor: Zhuojun Duan
Graphs Chapter 11 Objectives Upon completion you will be able to:
6.1.3 Graph representation.
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Lecture #13

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

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

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 )={,, } G1G1 complete graph G2G G3G3 incomplete graph

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

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

The Graph ADT A subgraph of G is a graph G’ such that V(G’)  V(G) and E(G’)  E(G) G1G G3G3

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

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

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

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 G3G3 not strongly connected strongly connected component (maximal strongly connected subgraph)

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

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 G3G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 directed graph G1G G2G2 1

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.

2 Graph Representations Adjacency Matrix Adjacency Lists Adjacency Multilists

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 G4G

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 )

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

G1G G3G G4G An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes Example

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

Graph Representations Example for Adjacency Multlists 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

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

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

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

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

Depth-First Search

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: visited:

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 BFS - A Graphical Representation d) c) b)a)a)

More BFS

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] visited: output: Breadth First Search : Example w outin

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?

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

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

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; } }

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

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

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

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.

Biconnected Graph Examples of Articulation Points (node 1, 3, 5, 7) Connected graph two connected components one connected graph

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