Graph Introduction, Searching Graph Theory Basics - Anil Kishore.

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.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graph Searching CSE 373 Data Structures Lecture 20.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
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.
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
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)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
Graph & BFS.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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.
COMP171 Depth-First Search.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 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.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Chapter 2 Graph Algorithms.
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,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
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.
Shortest Path Graph Theory Basics Anil Kishore.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSC212 Data Structure - Section AB
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.
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.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
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 Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Maximum Flow - Anil Kishore Graph Theory Basics. Prerequisites What is a Graph Directed, Weighted graphs How to traverse a graph using – Depth First Search.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Minimum Spanning Tree Graph Theory Basics - Anil Kishore.
Graphs Chapter 20.
Graphs Representation, BFS, DFS
Depth-First Search.
Graphs Representation, BFS, DFS
Chapter 11 Graphs.
Presentation transcript:

Graph Introduction, Searching Graph Theory Basics - Anil Kishore

Graph A collections of objects and pair wise relations between them A mathematical structure Objects are called Vertices ( or Nodes ) Relationship is shown using Edges

Graph Vertex Set V = { 1, 2, 3, 4, 5, 6} Edge Set E = { 12, 14, 34, 45, 46, 25, 56 } We use N and M for the corresponding sizes |V| = N, |E| = M

Storing a Graph How to store a Graph ? The two popular representations are – Adjacency Matrix N x N matrix A of 0s and 1s – Adjacency List A list of neighbors for each of the vertices

Adjacency Matrix A A[u][v] = 1, if vertex u and vertex v are adjacent = 0, otherwise Space : O(N 2 ) Symmetric Matrix

Adjacency List : { 2, 4 } 2 : { 1, 5 } 3 : { 4 } 4 : { 1, 3, 5, 6 } 5 : { 2, 4, 6 } 6 : { 4, 5 } Space : O(N+2M) Vertex u : list of all neighbors of u

Directions and Weights Edges and Vertices can have weight e.g.: length of the road, toll gate charge in a city. Edges can have direction ( like one-way roads )

Path, Cycle A Path of length n- 1 is a sequence of vertices u 1, u 2, …, u n such that vertices u i and u i+1 are adjacent o e.g. 1 – 2 – 5 – 4 – 3 is a path A Cycle of length n is a sequence of vertices u 1, u 2, …, u n such that vertices u i and u i+1 are adjacent and also u 1 and u n adjacent o e.g. 1 – 2 – 5 – 4 is a cycle

Power of Adjacency Matrix Number of paths ( possibly cyclic ) of length K from u to v F(u, v, K) = Sum of ( F(u, w, k-1) * F(w, v, 1) ) ( for all possible intermediate vertices w ) This is similar to the only computation step in Matrix Multiplication Note that F(u, v, 1) = A[u][v] A K [u][v] = Number of paths of length exactly K from u to v

Connected, Tree, Complete A graph is said to be connected if there exists a path between all pairs of vertices – How many minimum edges we need to make a n vertices graph connected ? – Cycles introduce redundant edges A Tree is a connected graph with out cycles ( acyclic ) – A tree on n vertices has exactly (n-1) edges A Complete Graph has all possible edges present in it – A complete on n vertices (K n ) has n C 2 edges

Traversing a graph Visit all the vertices in the graph in a particular order – Depth-first Search (DFS) visit child nodes before visiting sibling nodes – Breadth-first Search (BFS) visit sibling nodes before visiting child nodes

Depth-first Search algorithm DFS( u ) // start time of u Mark u as ‘visited’ FOR each node v ∈ Adj.List(u) IF NOT visited(v) THEN par[v] := u DFS(v) ENDIF ENDFOR //end time of u END-DFS Visit an unvisited neighbor, thus recursively traverse along depth of the graph par[v] denotes the first preceding vertex from which vertex v was visited, and defines a DFS tree Applications : Checking connectivity Finding Connected Components Topological ordering many more…

DFS

DFS TREE

DFS Recursive algorithm The active nodes in the recursion are Pushed and Popped, similar to a Stack Instead of recursion, can implement using a Stack data structure Complexity : O( N + M )

Breadth-first Search algorithm BFS( s ) Mark all vertices u ‘unvisited’ Create an empty queue Q EnQueue(s, Q) mask s as ‘visited’ WHILE NOT Empty(Q) DO u := DeQueue(Q) FOR each v ∈ Adj.List(u) DO IF NOT visited(v) EnQueue(v, Q) mask v as ‘visited’ ENDFOR ENDWHILE End-BFS Visit the vertices in the order encountered Vertices nearer to s are processed before farther ones Applications : Checking connectivity Finding Connected Components Shortest path in unweighted graphs many more…

BFS Q :

BFS Q : 1

BFS Q : u = 1

BFS Q : 2, 4 u = 1

BFS Q : 4 u = 2

BFS Q : 4, 5 u = 2

BFS Q : 5 u = 4

BFS Q : 5, 3, 6 u = 4

BFS Q : 3, 6 u = 5

BFS Q : 6 u = 3

BFS Q : u = 6

BFS Visit the nodes level by level ( level order traversal ) All nodes at level k are the ones with shortest path to s equals to k Complexity : O( N + M ) s

References Introduction to Algorithms – Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein - End -