Chapter 05 Introduction to Graph And Search Algorithms.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
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,
Graphs, BFS, DFS and More…
Foundations of Discrete Mathematics
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.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
1 Analysis of Algorithms Chapter - 06 Greedy Graph Algorithms.
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.
COSC 2007 Data Structures II Chapter 14 Graphs I.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Graph. 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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
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.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
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.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
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.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Finding Shortest Paths
Graphs Chapter 7 Visit for more Learning Resources.
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:

Chapter 05 Introduction to Graph And Search Algorithms

2  A Graph is a data structure which consists of a set of vertices, and a set of edges that connect (some of) them.  That is, G = ( V, E ), Where V - set of vertices, E - set of edges  The edges indicate how we can move through the graph Vertex (Node) Edge V = {1, 2, 3, 4, 5} E = { (1,2), (1,3), (1,4), (2,3), (3,5), (4,5) } What is Graph?

3  Computer Networks  Electrical Circuits  Road Map Computer Resistor/Inductor/… City Applications

4  A Directed Graph or Digraph is a graph where each edge has a direction  The edges in a digraph are called Arcs or Directed Edges  A directed graph edges’ allow travel in one direction  Example:  G = (V, E), where V = {1, 2, 3, 4, 5, 6} and E = {(1,4), (2,1), (2,3), (3,2), (4,3), (4,5), (4,6), (5,3), (6,1), (6,5)}  (1, 4) = 1→4 where 1 is the tail and 4 is the head Graph Categorization

5  An Undirected Graph is a graph where the edges have no directions  The edges in an undirected graph are called Undirected Edges  An undirected graph edges’ allow travel in either direction  Example:  G = (V, E), where V = {1, 2, 3, 4, 5} and E = {(1,2), (1,3), (1,4), (2,3), (3,5), (4,5)} Graph Categorization (Contd.)

 A Weighted Graph is a graph where all the edges are assigned weights.  If the same pair of vertices have more than one edge, that graph is called a Multigraph  Edges with the same end vertices are called parallel edges Graph Categorization (Contd.) 6

7  Adjacent vertices: :two vertices are adjacent if they are connected by an edge  An edge (i,j) is Incident to vertices i and j.  Vertices 2 and 5 are not adjacent  Loop or self edges: An edge ( i,i ) is called a self edge or a loop.  In graphs loops are not permitted  ( 1,1 ) and ( 4,4 ) are self edges Graph Terminology

8  Adjacent Edges: two non parallel edges are adjacent if they have vertex in common  An edge (i,j) is Incident to vertices i and j Graph Terminology

9  Path: A sequence of edges in the graph. A path is a subset of E that is a series of edges between two vertices  There can be more than one path between two vertices.  Vertex A is reachable from B if there is a path from A to B.  Paths from B to D  B, A, D  B, C, D  shortest path is the minimum number edges needed to connect two vertices  Simple Path: A path where all the vertices are distinct.( without loops and parallel edges)  1,4,5,3 is a simple path.  But 1,4,5,4 is not a simple path. A D C F E B G Graph Terminology (Contd.)

10  Length : Sum of the lengths of the edges on the path.  The length of a path in a graph is the number of edges in the path  Length of the path 1,4,5,3 is 3  Circuit: A path whose first and last vertices are the same.  The path 3,2,1,4,5,3 is a circuit.  Cycle: A circuit where all the vertices are distinct except for the first (and the last) vertex.  1,4,5,3,1 is a cycle, but 1,4,5,4,1 is not a cycle.  Hamiltonian Cycle: A Cycle that contains all the vertices of the graph.  1,4,5,3,2,1 is a Hamiltonian Cycle Graph Terminology (Contd.)

11  Degree of a Vertex : In an undirected graph, the no. of edges incident to the vertex  In-degree: The no. of edges entering the vertex in a digraph  Out-Degree: The no. of edges leaving the vertex in a digraph  In-degree of 1 is 3  Out-degree of 1 is 1  A Subgraph of graph G=(V,E) is a graph H=(U,F) such that U Є V and F Є E G=(V,E) H=(U,F) Graph Terminology (Contd.)

12  A graph is said to be Connected if there is at least one path from every vertex to every other vertex in the graph.  Tree: A connected undirected graph that contains no cycles  Forest: A graph that does not contain a cycle Connected Unconnected Tree Forest Graph Terminology (Contd.)

13  The Spanning Tree of a Graph G is a subgraph of G that is a tree and contains all the vertices of G Graph Spanning Tree Graph Terminology (Contd.)

14  Adjacency Matrix (A)  The Adjacency Matrix A=(a i,j ) of a graph G=(V,E) with n nodes is an nXn matrix  Each element of A is either 0 or 1, depending on the adjacency of the nodes  a ij = 1, if (i,j) Є E, = 0, otherwise  Example: Find the adjacency matrices of the following graphs Representation of Graphs

15  Adjacency Matrix of a Weighted Graph  The weight of the edge can be shown in the matrix when the vertices are adjacent  A nil value (0 or ∞) depending on the problem is used when they are not adjacent  Example: To find the minimum distance between nodes... ∞4∞ ∞∞5 95∞ Representation of Graphs (Contd.)

16  Adjacency List  An Adjacency list is an array of lists, each list showing the vertices a given vertex is adjacent to….  Adjacency List of a Weighted Graph  The weight is included in the list Representation of Graphs (Contd.)

Searching Graphs 17

 Why do we need to search graphs?  To find paths  To look for connectivity  Depth-First Search (DFS)  Start from an arbitrary node  Visit (Explore) an unvisited adjacent edge  If the node visited is a dead end, go back to the previous node (Backtrack)  Stop when no unvisited nodes are found and no backtracking can be done  Implemented using a Stack  Explore if possible, Backtrack otherwise… Depth-First Search 18

Algorithm DFS(G) { for each vertex u Є V[G] do { Color[u] := white; Parent[u] := nil; } for each vertex u Є V[G] do if (Color[u] = white) then DFS_Visit(u); } Algorithm DFS_Visit(u) { Color[u] := gray for each vertex v Є Adj[u] do if (Color[v] = white) then { Parent[v] := u; DFS_Visit(v); } Color[u] := black; } white - Unvisited gray - Discovered black - Finished DFS Algorithm 19

AC D B E H G F A, B, F, C, G, E Example 20

 Start from an arbitrary node  Visit all the adjacent nodes (distance=1)  Visit the nodes adjacent to the visited nodes (distance=2, 3 etc.)  Stop when a dead end is met  Implemented using a Queue  Explore all nodes at distance d… Breadth-First Search (BFS) 21

Algorithm BFS(G, s) { for each vertex u Є V[G] – {s} do { Color[u] := white; Distance[u] := ∞; Parent[u] := nil; } Color[s] := gray; Distance[s] := 0; Parent[s] := nil; Q := Ø; Enqueue (Q, s); while (Q ≠ Ø) do { u := Dequeue (Q); for each v Є Adj[u] do if (Color[v] = white) then { Color[v] := gray; Distance[v] := Distance[u] + 1; Parent[v] := u;Enqueue (Q,v); } Color[u] := black; } white - Unvisited gray - Discovered black - Finished s - Source Vertex Q - FIFO Queue BFS Algorithm 22

AC D B E H G F A, B, C, E, F, G Example and Analysis 23

Depth-first searching A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path For example, after searching A, then B, then D, the search backtracks and tries another path from B Node are explored in the order A B D E H L M N I O P C F G J K Q N will be found before J 24 LM N OP G Q H J IK FED BC A

Breadth-first searching A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away For example, after searching A, then B, then C, the search proceeds with D, E, F, G Node are explored in the order A B C D E F G H I J K L M N O P Q J will be found before N 25 LM N OP G Q H J IK FED BC A