Graphs Chapter 7 Visit for more Learning Resources.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Advertisements

Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
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)
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get,
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
© 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,
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.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Data Structures & Algorithms Graphs
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 Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Balanced Binary Search Trees
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures Graphs - Terminology
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Balanced Binary Search Trees
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Operations And Representation
Graphs Chapter 13.
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graph Operations And Representation
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs By Rajanikanth B.
Graph Implementation.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Graph Operations And Representation
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Graphs G = (V,E) V is the vertex set.
Chapter 9 Graph algorithms
Heaps Chapter 6 Section 6.9.
Presentation transcript:

Graphs Chapter 7 Visit for more Learning Resources

What is a graph? A data structure that consists of a set of finite nodes (V) and a set of edges(E) that relate the nodes to each other G=(V,E) V(G)=Vertices of graph G ,e.g. {1,2,3,4} E(G)=Edges of graph G ,e.g.{(1,2),(1,3),(3,4)} 1 4 2 3

Graphs u v Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are also called arcs and lines. Directed edge has an orientation (u,v). u v

Graphs Undirected graph => no oriented edge. Undirected edge has no orientation (u,v). u v Undirected graph => no oriented edge. Directed graph => every edge has an orientation.

Undirected Graph 2 3 8 10 1 4 5 9 11 6 7

Directed Graph (Digraph) 2 3 8 10 1 4 5 9 11 6 7

Applications—Communication Network 2 3 8 10 1 4 5 9 11 6 7 Internet connection. Vertices are computers. Send email from 1 to 7. Vertex = city, edge = communication link.

Driving Distance/Time Map 2 3 8 10 1 4 5 9 11 6 7 Vertex = city, edge weight = driving distance/time.

Street Map 2 3 8 10 1 4 5 9 11 6 7 Some streets are one way.

Complete Undirected Graph Has all possible edges. n = 2 n = 4 n = 3 n = 1

Number Of Edges—Undirected Graph Each edge is of the form (u,v) Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is the same as edge (v,u), the number of edges in a complete undirected graph is n(n-1)/2. Number of edges in an undirected graph is <= n(n-1)/2.

Number Of Edges—Directed Graph Each edge is of the form (u,v), u != v. Number of such pairs in an n vertex graph is n(n-1). Since edge (u,v) is not the same as edge (v,u), the number of edges in a complete directed graph is n(n-1). Number of edges in a directed graph is <= n(n-1).

Graph terminology 1.Adjacent nodes: two nodes are adjacent if they are connected by an edge 2.Path: A sequence of edges that connect two nodes in a graph 3.Path Length: Total no of edges included in path. 4.Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5

Graph terminology (cont.) What is the number of edges in a complete directed graph with N vertices?  N * (N-1)

Graph terminology (cont.) What is the number of edges in a complete undirected graph with N vertices?  N * (N-1) / 2

5. Vertex Degree OR Degree of Node 2 3 8 10 1 4 5 9 11 6 7 Number of edges incident to vertex. degree(2) = 2, degree(5) = 3, degree(3) = 1

6.In-Degree Of A Vertex 2 3 8 10 1 4 5 9 11 6 7 in-degree is number of incoming edges indegree(2) = 1, indegree(8) = 0

7.Out-Degree Of A Vertex 2 3 8 10 1 4 5 9 11 6 7 out-degree is number of outbound edges outdegree(2) = 1, outdegree(8) = 2

Graph terminology (cont.) 8.Source:A Node which has only outgoing edges. 9.Sink: A Node which has only Incoming edges. 10. Pendant Node: if indegree of node is 1 and outdegree is 0 then it is pendant Node. 11. Reachable: If path exist between two nodes then they are reachable.

Graph terminology (cont.) 12. Sling or loop: An edge of graph which joins a node to itself is called sling or loop. 13.Parallel Edges: The two distinct edges between a pair to nodes which are opposite in a direction. 14.Weighted Edge: edge has Numerical Values. 15.Isolated Node: Not an Adjacent node of any other node

Graph terminology (cont.) 16. Weighted graph: a graph in which each edge carries a value 2 3 8 10 1 4 5 9 11 6 7

Graph terminology (cont.) 17. Isolated Graph: A Graph which contain only isolated node. 18. Directed Acyclic Graph: A diagraph with no cycles. 2 1 4 5

Graph implementation Array-based implementation A 1D array is used to represent the vertices A 2D array (adjacency matrix) is used to represent the edges

Graph Representation There are two ways to represent the Graph: Sequential Representation Adjacency Matrix Path Matrix Adjacency Lists/Linked List representation.

1.Adjacency Matrix 0/1 n x n matrix, where n = # of vertices A(i,j) = 1 iff (i,j) is an edge 2 3 1 4 5 1 2 3 4 5 1 1 1 1 1 1 1 1 1 1

Path Matrix

Path Matrix

Adjacency Lists aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists. aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3) 2 3 1 4 5

Adjacency Lists Representation A graph of n nodes is represented by a one-dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order

Example of List Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5 L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5

Linked Adjacency Lists Each adjacency list is a chain. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3 Array length n simply means we need an array with n spots. A direct implementation using a Java array would need n+1 spots, because spot 0 would not be utilized. However, by using spot 0 for vertex 1, spot 1 for vertex 2, and so on, we could get by with a Java array whose length is actually n. Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)

Array Adjacency Lists aList[1] [2] [3] [4] aList[5] Array Length = n Each adjacency list is an array list. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] Array Length = n # of list elements = 2e (undirected graph) # of list elements = e (digraph)

Graph Search (traversal) How do we search a graph? At a particular vertices, where shall we go next? Two common Methods: Breadth-first search (BFS) Depth-first search (DFS) In BFS, one explore a graph level by level away (explore all neighbors first and then move on) In DFS, go as far as possible along a single path until reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrack Data Structure and Algorithm

A vertex u is reachable from vertex v iff there is a path from v to u. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. 2 3 8 10 1 4 5 9 11 6 7

Graph Search Methods A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v. 2 3 8 10 1 4 5 9 11 6 7 Visit, mark, and label used as synonyms.

Breadth-First Search Visit start vertex and put into a FIFO queue. Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue.

Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Start search at vertex 1.

Breadth-First Search Example FIFO Queue 1 2 3 8 1 1 4 5 9 10 6 7 11 Visit/mark/label start vertex and put in a FIFO queue.

Breadth-First Search Example FIFO Queue 1 2 3 8 1 1 4 5 9 10 6 7 11 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 2 4 8 1 1 4 4 5 9 10 6 7 11 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 2 4 8 1 1 4 4 5 9 10 6 7 11 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 4 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 4 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 5 3 6 8 1 1 4 4 5 5 9 10 6 6 7 11 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 6 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 9 7 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 7 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 7 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 8 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Remove 8 from Q; visit adjacent unvisited vertices; put in Q.

Breadth-First Search Example 2 FIFO Queue 2 3 3 8 8 1 1 4 4 5 5 9 9 10 6 6 7 7 11 Queue is empty. Search terminates.

Breadth-First Search Property All vertices reachable from the start vertex (including the start vertex) are visited.

Breadth-first Search (BFS) Search for all vertices that are directly reachable from the root (called level 1 vertices) After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on. Data Structure and Algorithm

Example r s t u         v w x y Data Structure and Algorithm

Example        Q: s r s t u r s t u v w x y v w x y       v w x y v w x y Q: s Data Structure and Algorithm

Example 1    1   Q: w r r r s s t t u u v v w w x x y y    1   v v w w x x y y Q: w r Data Structure and Algorithm

Example 1 2   1 2  Q: r t x r s t u r s t u v w x y v w x y 2   1 2  v w x y v w x y Q: r t x Data Structure and Algorithm

Example 1 2  2 1 2  Q: t x v r s t u r s t u v w x y v w x y 2  2 1 2  v w x y v w x y Q: t x v Data Structure and Algorithm

Example 1 2 3 2 1 2  Q: x v u r s t u r s t u v w x y v w x y 2 3 2 1 2  v w x y v w x y Q: x v u Data Structure and Algorithm

Example 1 2 3 2 1 2 3 Q: v u y r s t u r s t u v w x y v w x y 2 3 2 1 2 3 v w x y v w x y Q: v u y Data Structure and Algorithm

Example 1 2 3 2 1 2 3 Q: u y r s t u r s t u v w x y v w x y 2 3 2 1 2 3 v w x y v w x y Q: u y Data Structure and Algorithm

Example 1 2 3 2 1 2 3 Q: y r s t u r s t u v w x y v w x y 2 3 2 1 2 3 v w x y v w x y Q: y Data Structure and Algorithm

All Vertices are Visited…….. Example r s t u r s t u 1 2 3 2 1 2 3 v w x y v w x y All Vertices are Visited…….. Q: Ø Data Structure and Algorithm

Depth-First Search (DFS) The basic idea behind this algorithm is that it traverses the graph using recursion Go as far as possible until you reach a dead end Backtrack to the previous path and try the next branch The graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, j d a b h i c g e j f Data Structure and Algorithm

Depth-First Search depthFirstSearch(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) depthFirstSearch(u); } Note that vertices adjacent from v are examined one at a time. As soon as an unreached adjacent vertex u is found, a depthFirstSearch(u) is done. Remaining vertices adjacent from v are examined after depthFirstSearch(u) completes.

Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 2 1 Start search at vertex 1. Label vertex 1 and do a depth first search from either 2 or 4. Suppose that vertex 2 is selected.

Depth-First Search Example 2 2 2 3 8 1 1 4 5 5 9 10 6 7 11 Label vertex 2 and do a depth first search from either 3, 5, or 6. Suppose that vertex 5 is selected.

Depth-First Search Example 2 2 2 3 8 1 1 4 5 5 5 9 9 10 6 7 11 Label vertex 5 and do a depth first search from either 3, 7, or 9. Suppose that vertex 9 is selected.

Depth-First Search Example 2 2 2 3 8 8 1 1 4 5 5 5 9 9 9 10 6 7 11 Label vertex 9 and do a depth first search from either 6 or 8. Suppose that vertex 8 is selected.

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 5 5 5 9 9 9 10 6 6 7 11 Label vertex 8 and return to vertex 9. From vertex 9 do a dfs(6).

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 5 5 5 9 9 9 10 6 6 6 7 11 Label vertex 6 and do a depth first search from either 4 or 7. Suppose that vertex 4 is selected.

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 11 Label vertex 4 and return to 6. From vertex 6 do a dfs(7).

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Label vertex 7 and return to 6. Return to 9.

Depth-First Search Example 2 2 2 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to 5.

Depth-First Search Example 2 2 2 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Do a dfs(3).

Depth-First Search Example 2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Label 3 and return to 5. Return to 2.

Depth-First Search Example 2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to 1.

Depth-First Search Example 2 2 2 3 3 3 8 8 8 1 1 4 4 4 5 5 5 9 9 9 10 6 6 6 7 7 7 11 Return to invoking method. For more detail contact us