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.

Slides:



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

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Review Binary Search Trees Operations on Binary Search Tree
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Part A - Terminology and Traversals
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
Breadth-First and Depth-First Search
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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.
CS171 Introduction to Computer Science II Graphs Strike Back.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 9 Instructor: Paul Beame.
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.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 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.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
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,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CSC212 Data Structure - Section AB
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 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:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs.
Graphs Part 2 Adjacency Matrix
Data Structures and Algorithm Analysis Graph Algorithms
Analysis and design of algorithm
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:

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

Data Structures and Algorithms Ver. 1.0 Session 17 To implement a graph, you need to first represent the given information in the form of a graph. The two most commonly used ways of representing a graph are as follows: Adjacency Matrix Adjacency List Representing a Graph

Data Structures and Algorithms Ver. 1.0 Session 17 Consider the following graph: Adjacency Matrix Adjacency Matrix Representation v1 v2 v3 v4 v v v v

Data Structures and Algorithms Ver. 1.0 Session 17 Consider the following graph: Adjacency List Adjacency List Representation

Data Structures and Algorithms Ver. 1.0 Session 17 Traversing a graph means visiting all the vertices in a graph. You can traverse a graph with the help of the following two methods: Depth First Search (DFS) Breadth First Search (BFS) Traversing a Graph

Data Structures and Algorithms Ver. 1.0 Session 17 Algorithm: DFS(v) 1.Push the starting vertex, v into the stack. 2.Repeat until the stack becomes empty: DFS a.Pop a vertex from the stack. b.Visit the popped vertex. c.Push all the unvisited vertices adjacent to the popped vertex into the stack.

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) v1 Push the starting vertex, v1 into the stack

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) v1 Pop a vertex, v1 from the stack Visit v1 Push all unvisited vertices adjacent to v1 into the stack v1 Visited:

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) v4 Pop a vertex, v1 from the stack Visit v1 Push all unvisited vertices adjacent to v1 into the stack v1 Visited: v2

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v2 from the stack Visit v2 Push all unvisited vertices adjacent to v2 into the stack v1v2 v4 v2

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v2 from the stack Visit v2 Push all unvisited vertices adjacent to v2 into the stack v1v2 v3 v6 v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v6 from the stack Visit v6 Push all unvisited vertices adjacent to v6 into the stack v1v2v6 There are no unvisited vertices adjacent to v6 v3 v6 v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v3 from the stack Visit v3 Push all unvisited vertices adjacent to v3 into the stack v1v2v6v3 v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v3 from the stack Visit v3 Push all unvisited vertices adjacent to v3 into the stack v1v2 v5 v6v3 v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v5 from the stack Visit v5 Push all unvisited vertices adjacent to v5 into the stack v1v2v6v3v5 There are no unvisited vertices adjacent to v5 v5 v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: Pop a vertex, v4 from the stack Visit v4 Push all unvisited vertices adjacent to v4 into the stack v1v2v6v3v5 There are no unvisited vertices adjacent to v4 v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Visited: The stack is now empty Therefore, traversal is complete v1v2v6v3v5v4

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) Although the preceding algorithm provides a simple and convenient method to traverse a graph, the algorithm will not work correctly if the graph is not connected. In such a case, you will not be able to traverse all the vertices from one single starting vertex.

Data Structures and Algorithms Ver. 1.0 Session 17 DFS (Contd.) To solve this problem, you need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph. 1.Repeat step 2 for each vertex, v in the graph 2.If v is not visited: a. Call DFS(v)

Data Structures and Algorithms Ver. 1.0 Session 17 Algorithm: BFS(v) 1.Visit the starting vertex, v and insert it into a queue. 2.Repeat step 3 until the queue becomes empty. 3.Delete the front vertex from the queue, visit all its unvisited adjacent vertices, and insert them into the queue. BFS

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Visit v1 Insert v1 into the queue v1

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v1 from the queue Visit all unvisited vertices adjacent to v1 and insert them in the queue v1 Visited:

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v1 from the queue Visit all unvisited vertices adjacent to v1 and insert them in the queue v2 v1v2 v4

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v2 from the queue Visit all unvisited vertices adjacent to v2 and insert them in the queue v2 v1v2 v4

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v2 from the queue Visit all unvisited vertices adjacent to v2 and insert them in the queue v1v2 v4 v3 v6

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v4 from the queue Visit all unvisited vertices adjacent to v4 and insert them in the queue v1v2 v4 v3 v6 v5

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v3 from the queue Visit all unvisited vertices adjacent to v3 and insert them in the queue v1v2 v4 v3 v6 v5 v3 does not have any unvisited adjacent vertices

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v6 from the queue Visit all unvisited vertices adjacent to v6 and insert them in the queue v1v2 v4 v3v6 v5 v3 does not have any unvisited adjacent vertices

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v6 from the queue Visit all unvisited vertices adjacent to v6 and insert them in the queue v1v2 v4 v3v6v5 v6 does not have any unvisited adjacent vertices

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v5 from the queue Visit all unvisited vertices adjacent to v5 and insert them in the queue v1v2 v4 v3v6v5 v6 does not have any unvisited adjacent vertices

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Remove a vertex v5 from the queue Visit all unvisited vertices adjacent to v5 and insert them in the queue v1v2 v4 v3v6v5 v5 does not have any unvisited adjacent vertices

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) The queue is now empty Therefore, traversal is complete v1v2 v4 v3v6v5 v5 does not have any unvisited adjacent vertices

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) Although the preceding algorithm provides a simple and convenient method to traverse a graph, the algorithm will not work correctly if the graph is not connected. In such a case, you will not be able to traverse all the vertices from one single starting vertex.

Data Structures and Algorithms Ver. 1.0 Session 17 BFS (Contd.) To solve this problem, you need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph. 1.Repeat step 2 for each vertex, v in the graph 2.If v is not visited: a. Call BFS(v)

Data Structures and Algorithms Ver. 1.0 Session 17 Problem Statement: You have to represent a set of cities and the distances between them in the form of a graph. Write a program to represent the graph in the form of an adjacency matrix. Activity: Implementing a Graph by Using Adjacency Matrix Representation

Data Structures and Algorithms Ver. 1.0 Session 17 Many problems can be easily solved by reducing them in the form of a graph Graph theory has been instrumental in analyzing and solving problems in areas as diverse as computer network design, urban planning, finding shortest paths and molecular biology. Applications of Graphs

Data Structures and Algorithms Ver. 1.0 Session 17 Solving the Shortest Path Problem The shortest path problem can be solved by applying the Dijkstra’s algorithm on a graph The Dijkstra’s algorithm is based on the greedy approach The steps in the Dijkstra’s algorithm are as follows: 1. Choose vertex v corresponding to the smallest distance recorded in the DISTANCE array such that v is not already in FINAL. 2. Add v to FINAL. 3. Repeat for each vertex w in the graph that is not in FINAL: a. If the path from v1 to w via v is shorter than the previously recorded distance from v1 to w (If ((DISTANCE[v] + weight of edge(v,w)) < DISTANCE[w])): i. Set DISTANCE[w]=DISTANCE[v] + weight of edge(v,w). 4. If FINAL does not contain all the vertices, go to step 1.

Data Structures and Algorithms Ver. 1.0 Session 17 Solving the Shortest Path Problem (Contd.) DISTANCE v1v2v3v4v5v6 FINAL 0 ∞ 3∞∞ 5 v1 Suppose you need to find the shortest distance of all the vertices from vertex v1. Add v1 to the FINAL array.

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 0 ∞ 3∞∞ 5 v1 In the DISTANCE array, vertex v4 has the shortest distance from vertex v1. Therefore, v4 is added to the FINAL array. v4 Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 0 ∞ 3∞∞ 5 v1v4 v1 → v2 = 5 v1 → v4 → v2 = 3 + ∞ = ∞ ∞ > 5 Therefore, no change is made. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 0 ∞ 3∞∞ 5 v1v4 Therefore, the entry corresponding to v3 in the DISTANCE array is changed to 5. v1 → v3 = ∞ v1 → v4 → v3 = = 5 5 < ∞ Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session 17 ∞ v1v2v3v4v5v6 03∞ 5 v1v4 5 v1 → v5 = ∞ v1 → v4 → v5 = = 9 9 < ∞ Therefore, the entry corresponding to v5 in the DISTANCE array is changed to 9. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 03∞ 5 v1v4 5 9 Both the values are equal. Therefore, no change is made. v1 → v6 = ∞ v1 → v4 → v6 = 3 + ∞ = ∞ PASS 1 complete Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 03∞ 5 v1v4 5 9 From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not in the FINAL array. v2 and v3 have the shortest and the same distance from v1. Let us select v2 and add it to the FINAL array. v2 Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 03∞ 5 v1v4 5 9 v2 v1 → v3 = 5 v1 → v2 → v3 = = 9 9 > 5 Therefore, no change is made. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v6 03∞ 5 v1v4 5 9 v2 v1 → v5 = 9 v1 → v2 → v5 = 5 + ∞ = ∞ ∞ > 9 Therefore, no change is made. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session 17 ∞ v1v2v3v4v5v v1v4 5 9 v2 v1 → v6 = ∞ v1 → v2 → v6 = = < ∞ Therefore, the entry corresponding to v6 in the DISTANCE array is changed to 11. Pass 2 complete Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v v1v4 5 9 v2 11 v3 From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not in the FINAL array. Let us select v3 and add it to the FINAL array. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v v1v4 5 9 v2 11 v3 8 v1 → v5 = 9 v1 → v3 → v5 = = 8 8 < 9 Therefore, the entry corresponding to v5 in the DISTANCE array is changed to 8. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v v1v4 5 v2v3 8 v1 → v6 = 11 v1 → v3 → v6 = = 8 8 < 11 Therefore, the entry corresponding to v6 in the DISTANCE array is changed to 8. Pass 3 complete Solving the Shortest Path Problem (Contd.) DISTANCE FINAL 11

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v v1v4 5 v2v3 8 8 v5 From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not in the FINAL array. Let us select v5 and add it to the FINAL array. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v v1v4 5 v2v3 8 8 v5 v1 → v6 = 8 v1 → v5 → v6 = 8 + ∞ = ∞ ∞ > 8 Therefore, no change is made. Pass 4 complete Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session v1v2v3v4v5v v1v4 5 v2v3 8 8 v5 Now add the only remaining vertex, v6 to the FINAL array. v6 All vertices have been added to the FINAL array. This means that the DISTANCE array now contains the shortest distances from vertex v1 to all other vertices. Solving the Shortest Path Problem (Contd.) DISTANCE FINAL

Data Structures and Algorithms Ver. 1.0 Session 17 Problem Statement: In the previous activity, you created a program to represent a set of cities and the distances between them in the form of a graph. Extend the program to include the functionality for finding the shortest path from a given city to all the other cities. Activity: Solving the Shortest Path Problem

Data Structures and Algorithms Ver. 1.0 Session 17 In this session, you learned that: The two most commonly used ways of representing a graph are as follows: Adjacency matrix Adjacency list Traversing a graph means visiting all the vertices in the graph. In a graph, there is no special vertex designated as the starting vertex. Therefore, traversal of the graph may start from any vertex. You can traverse a graph with the help of the following two methods: DFS BFS Summary

Data Structures and Algorithms Ver. 1.0 Session 17 Graph theory has been instrumental in analyzing and solving problems in areas as diverse as computer network design, urban planning, finding shortest paths and molecular biology. Summary (Contd.)