Graph Implementation.

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

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 Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 24 Graphs.
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.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
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.
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 & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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,
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Graph. Terminologi Graph A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices. – An edge e = (vi,vj) connects.
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.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs Chapter 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Podcast Ch24c Title: Breadth First Search
BCA-II Data Structure Using C Submitted By: Veenu Saini
Chapter 22 Elementary Graph Algorithms
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
Data Structures 13th Week
Csc 2720 Instructor: Zhuojun Duan
Common final examinations
Data Structures and Algorithms for Information Processing
Graph Operations And Representation
Data Structures for Java William H. Ford William R. Topp
Graph Implementations
Graphs Graph transversals.
Graphs Representation, BFS, DFS
Graphs Chapter 11 Objectives Upon completion you will be able to:
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 22: Elementary Graph Algorithms
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Spanning Trees Longin Jan Latecki Temple University based on slides by
Data Structures & Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 16 1 – Graphs Graph Categories Strong Components
Data structure for graph algorithms: Adjacent list, Adjacent matrix
CE 221 Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Elementary Graph Algorithms
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:

Graph Implementation

Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices. An edge e = (vi, vj) connects vertices vi and vj. A self-loop is an edge that connects a vertex to itself. We assume that none of our graphs have self-loops. Vertices = {v1, v2, v3, …, vm} Edges = {e1, e2, e3, …, en}

Graph Terminology (continued) The degree of a vertex is the number of edges originating at the vertex. Two vertices in a graph are adjacent (neighbors) if there is an edge connecting the vertices. A path between vertices v and w is a series of edges leading from v to w. The path length is the number of edges in the path.

Graph Terminology (continued) A graph described until now is termed an undirected graph. Movement between vertices can occur in either direction. In a digraph, edges have a direction. There might be an edge from v to w but no edge from w to v.

Graph Terminology (continued)

Graph Terminology (continued) In a digraph, a directed path (path) connecting vertices vs and ve is a sequence of directed edges that begin at vs and end at ve. The number of the edges that emanate from a vertex v is called the out-degree of the vertex. The number of the edges that terminate in vertex v is the in-degree of the vertex.

7 Main Index Contents Adjacency Matrix An m by m matrix, called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (v,, vj). Its value is the weight of the edge, or -1 if the edge does not exist. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

8 Main Index Contents Adjacency List

Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the vertices. From any starting vertex in a graph, it might not be possible to search all of the vertices. A graph could have a cycle that results in multiple visits to a vertex.

2 Major Traversal Algorithms Breadth-first search visits vertices in the order of their path length from a starting vertex. It may not visit every vertex of the graph Depth-first search traverses all the vertices of a graph by making a series of recursive calls that follow paths through the graph.

Processing the Traversal To simplify the search graph algorithms discern the state of a vertex during the algorithm by using the colors WHITE, GRAY, and BLACK.

Breadth-First Traversal Process and mark the starting vertex and place it in a queue Remove a vertex (v) from the front of the queue For each unmarked neighbor (u) of v: Process u, mark u, and place u in the queue. GOTO 2 (Recursive visit of unmarked neigbors!).

Example: Breadth-First Traversal Vertices are visited in the order A,B,C,G,D,E,F

Breadth-First Traversal (continued) Color all vertices of the sample graph WHITE and push the starting vertex (A) onto the queue visitQueue. Pop A from the queue, color it BLACK, and insert it into visitList, which is the list of visited vertices. Push all WHITE neighbors onto the queue.

Breadth-First Traversal (continued) Pop B from the queue and place it in visitList with color BLACK. The only adjacent vertex for B is D, which is still colored WHITE. Color D GRAY and add it to the queue

Breadth-First Traversal (continued) Pop C and place it in visitList. The adjacent vertex G is GRAY. No new vertices enter the queue.

Breadth-First Traversal (continued) Pop vertex G from the queue and place it in visitList. G has no adjacent vertices, so pop D from the queue. The neighbors, E and F, enter the queue.

Breadth-First Traversal (continued) Continue in this fashion until the queue is empty.

Depth-First Traversal The depth-first visit algorithm moves along a directed edge to one of the start vertex’s neghbors It moves from here to other directed edges until the end of a path (a terminal vertex is a vertex that has no neighbours or only neighbours that are BLACK). At the terminal it backs up to parent vertex to find whether the parent has any more unmarked neighbors, if so continue searching.

Depth-First Traversal (continued) Color all vertices WHITE. A vertex is colored GRAY when it is first contacted in a recursive descent. It becomes BLACK only when the vertex is actually visited. Begin at a starting vertex and search down paths of neighbors until reaching a vertex that has no neighbors or only neighbors that are BLACK. At this point, a visit occurs at the "terminal" vertex and it becomes BLACK.

Depth-First Traversal (continued) Backtrack to the previous recursive step and look for another adjacent vertex and launch a scan down its paths. There is no ordering among vertices in an adjacency list, so the paths and hence the order of visits to vertices can vary.

Depth-First Traversal (continued) Discover A (color GRAY) Discover B (color GRAY) Discover D (color GRAY) Discover E (color GRAY, then BLACK) Backtrack D (color BLACK) Backtrack B (color BLACK) Backtrack A (A remains GRAY) Discover C (color BLACK) Backtrack A (color BLACK). Visit complete.