COSC 2007 Data Structures II

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Review Binary Search Trees Operations on Binary Search Tree
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
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.
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)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
CS202 - Fundamental Structures of Computer Science II
Graphs.
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.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
COSC 2007 Data Structures II Chapter 14 Graphs III.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
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.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs – Adjacency Matrix
Graphs and Paths : Chapter 15 Saurav Karmakar
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:
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Chapter 20.
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures Graphs - Terminology
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs – Adjacency Matrix
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Graph Implementation.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

COSC 2007 Data Structures II April 26, 2017 COSC 2007 Data Structures II Chapter 14 Graphs II Graphs

Topics Traversal Graph implementation Adjacency matrix adjacency list Depth-first search Breadth-first search

ADT Graphs Insertion & deletion operations are somewhat different for graphs than for other ADTs in that they apply to either nodes or edges Elements: A graph consists of nodes and edges. Each node will contain one data element Each node is uniquely identified by the key value of the element it contains Structure: An edge is a one-to-one relationship between a pair of distinct nodes A pair of nodes can be connected by at most one edge, but any node can be connected to any collection of other nodes

ADT Graphs Operations: Create an empty graph Determine whether a graph is empty Determine the number of nodes in a graph Determine the number of edges in a graph Determine whether an edge exists between two nodes Insert a node/ an edge Delete a node/ an edge Retrieve a node having a given search key Traverse a graph

Implementation of Graphs Two common implementations: Adjacency matrix Adjacency list Adjacency Matrix (Incidence Matrix) A graph containing n nodes can be represented by an n x n matrix of Boolean values or 1/0 M[i,j] = 1 (T) <==> If there is an edge between node i & node j of the graph. 0 (F) Otherwise

Implementation of Graphs Adjacency Matrix Example: A B L P C R A B L C R P T

Implementation of Graphs Adjacency Matrix The ith row and ith column are identical (Symmetric matrix) Fewer than half the entries are needed, since each edge is recorded twice and the diagonal contains all zeros Using a lower triangular matrix is possible, but it is not convenient If the graph is directed, the full matrix, except the diagonal, is needed

Implementation of Graphs Adjacency Lists Keep an array of linked lists, one points to one LL For each node, in the linked list, there is an edge list of the neighbors of that node Any other possibility?

Implementation of Graphs Maintain each adjacency list as a linked chain Array h head nodes keeps track of the lists h[i] points to the first node of adjacency list for vertex i G 1 2 3 4

Implementation of Graphs Questions:

2 3 1 4 5 Graph Traversal Process each node in a graph exactly once (if & only if the graph is connected) The order of visiting the nodes is not unique, because it depends on the representation

A B C F D E Graph Traversal An adjacency list representation of the graph could lead to many different orderings of the visit, depending on the sequence in which the edges are entered If a graph contains a cycle, a graph-traversal algorithm can loop indefinitely, unless the visited nodes are marked

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

COSC 2007 April 26, 2017 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. Graphs

Graph Search Methods Many graph problems solved using a search method Path from one vertex to another Is the graph connected? Etc. Commonly used search methods: Depth-first search Breadth-first search

Graph Traversal Depth First Search (DFS) Idea of DFS algorithm Follow a path from a previous node as far as possible before moving to the next neighbor of the starting node DFS uses a stack to put all nodes to be traversed (ready stack). Idea of DFS algorithm “Mark" a node as soon as we visit it Try to move to an unmarked adjacent node If there are no unmarked nodes at the present position, backtrack along the nodes already visited, until a node is reached that is adjacent to an unvisited node Visit this node Continue the process

Graph Traversal Depth First Search (DFS) Recursive dfs(in v:Vertex) // Traverses a graph beginning at node v by using a // depth-first search (Recursive version) Mark v as visited for (each unvisited node u adjacent to v) dfs(u)

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

Graph Traversal dfs(v) Depth First Search (DFS) Iterative (Using a Stack) dfs(v) // Traverses a graph beginning at node v by using a DFS s.CreateStack( ) s.push ( v) // Push v onto stack & mark it Mark v as visited while (!s.isEmpty( ) ) { if (no unvisited nodes are adjacent to v on stack top) s.pop () // Backtrack else { Select an unvisited node u adjacent to the node on top of stack s.push (u) Mark u as visited } // end if } // end while

Graph Traversal Breadth First Search (BFS) Look at all of the neighbors of a node first, then follow the paths from each of these nodes to its neighbors, and so on Nodes that have been visited but not explored are kept in a (ready) queue, so that when we are ready to move to a node adjacent to the current node, we will be able to return to another node adjacent to the old current node after our move

Graph Traversal Breadth First Search (BFS) Iterative: Using Queue bfs( v:Vertex) // Traverses a graph beginning at node v by using a // Breadth-first search q.createQueue ( ) q.enqueue(v) // Add v to queue & mark it Mark v as visited while (!q.isEmpty ( ) ) { q.dequeue(w) for (each unvisited node u adjacent to w) { Mark u as visited q.enqueue(u) } // end for } // end while

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.

Review A graph-traversal algorithm stops when ______. it first encounters the designated destination vertex it has visited all the vertices that it can reach it has visited all the vertices it has visited all the vertices and has returned to the vertex that it started from

Review A ______ is the subset of vertices visited during a traversal that begins at a given vertex. circuit multigraph digraph connected component

Review An iterative DFS traversal algorithm uses a(n) ______. list array Queue stack

Review An iterative BFS traversal algorithm uses a(n) ______. list array Queue stack

Review A ______ is an undirected connected graph without cycles. tree multigraph digraph connected component

Review A connected undirected graph that has n vertices must have at least ______ edges. n n – 1 n / 2 n * 2

Review A connected undirected graph that has n vertices and exactly n – 1 edges ______. cannot contain a cycle must contain at least one cycle can contain at most two cycles must contain at least two cycles

Review A tree with n nodes must contain ______ edges. n n – 1 n – 2