Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
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 Searching CSE 373 Data Structures Lecture 20.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Graphs CS3240, L. grewe.
#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Using Search in Problem Solving
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
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.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphs & Graph Algorithms 2
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 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS261 Data Structures DFS and BFS – Edge List Representation.
Tree Searching Breadth First Search Dept First Search.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
© 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,
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.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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.
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.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
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.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
Graphs Representation, BFS, DFS
Graphs & Graph Algorithms 2
Chapter 11 Graphs.
Graph Implementation.
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:

Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park

Graph Data Structures Many-to-many relationship between elements Each element has multiple predecessors Each element has multiple successors

Graph Definitions Node Element of graph State List of adjacent nodes Edge Connection between two nodes State Endpoints of edge A

Graph Definitions Directed graph Directed edges Undirected graph Undirected edges

Graph Definitions Weighted graph Weight (cost) associated with each edge

Graph Definitions Path Sequence of nodes n 1, n 2, … n k Edge exists between each pair of nodes n i, n i+1 Example A, B, C is a path

Graph Definitions Path Sequence of nodes n 1, n 2, … n k Edge exists between each pair of nodes n i, n i+1 Example A, B, C is a path A, E, D is not a path

Graph Definitions Cycle Path that ends back at starting node Example A, E, A

Graph Definitions Cycle Path that ends back at starting node Example A, E, A A, B, C, D, E, A Simple path No cycles in path Acyclic graph No cycles in graph

Graph Definitions Reachable Path exists between nodes Connected graph Every node is reachable from some node in graph Unconnected graphs

Graph Operations Traversal (search) Visit each node in graph exactly once Usually perform computation at each node Two approaches Breadth first search (BFS) Depth first search (DFS)

Breadth-first Search (BFS) Approach Visit all neighbors of node first View as series of expanding circles Keep list of nodes to visit in queue Example traversal n a, c, b e, g, h, i, j d, f

Breadth-first Search (BFS) Example traversals starting from Left to rightRight to leftRandom

traversals orders left to right or right to left doesn’t make a lot of sense for graphs Each node just has a set of successors and predecessors; no order among edges For breadth first search visit all nodes at distance k from starting point before visiting any nodes at (minimum) distance k+1 from starting point

Depth-first Search (DFS) Approach Visit all nodes on path first Backtrack when path ends Keep list of nodes to visit in a stack Example traversal n, a, b, c, d, … f …

Depth-first Search (DFS) Example traversals from Left to rightRight to leftRandom

Traversal Algorithms Issue How to avoid revisiting nodes Infinite loop if cycles present Approaches Record set of visited nodes Mark nodes as visited ?5 ?

Traversal – Avoid Revisiting Nodes Record set of visited nodes Initialize { Visited } to empty set Add to { Visited } as nodes is visited Skip nodes already in { Visited } V =  V = { 1 } V = { 1, 2 }

Traversal – Avoid Revisiting Nodes Mark nodes as visited Initialize tag on all nodes (to False) Set tag (to True) as node is visited Skip nodes with tag = True F F F F T F F F T T F F

Traversal Algorithm Using Sets { Visited } =  { Discovered } = { 1st node } while ( { Discovered }   ) take node X out of { Discovered } if X not in { Visited } add X to { Visited } for each successor Y of X if ( Y is not in { Visited } ) add Y to { Discovered }

Traversal Algorithm Using Tags for all nodes X set X.tag = False { Discovered } = { 1st node } while ( { Discovered }   ) take node X out of { Discovered } if (X.tag = False) set X.tag = True for each successor Y of X if (Y.tag = False) add Y to { Discovered }

BFS vs. DFS Traversal Order nodes taken out of { Discovered } key Implement { Discovered } as Queue First in, first out Traverse nodes breadth first Implement { Discovered } as Stack First in, last out Traverse nodes depth first

BFS Traversal Algorithm for all nodes X X.tag = False put 1 st node in Queue while ( Queue not empty ) take node X out of Queue if (X.tag = False) set X.tag = True for each successor Y of X if (Y.tag = False) put Y in Queue

DFS Traversal Algorithm for all nodes X X.tag = False put 1 st node in Stack while (Stack not empty ) pop X off Stack if (X.tag = False) set X.tag = True for each successor Y of X if (Y.tag = False) push Y onto Stack

Recursive DFS Algorithm Traverse( ) for all nodes X set X.tag = False Visit ( 1 st node ) Visit ( X ) set X.tag = True for each successor Y of X if (Y.tag = False) Visit ( Y )