#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching.

Slides:



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

Chapter 9: Graphs Shortest Paths
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
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
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
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.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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 reserved14 B-1 Chapter 14 (continued) Graphs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
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.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPH Learning Outcomes Students should be able to:
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Spring 2015 Lecture 10: Elementary Graph Algorithms
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,
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
Graphs 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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++
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Eulerian Paths and Cycles. What is a Eulerian Path Given an graph. Find a path which uses every edge exactly once. This path is called an Eulerian Path.
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.
Graphs Upon completion you will be able to:
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.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
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.
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.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 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.
Graph Algorithms BFS, DFS, Dijkstra’s.
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graphs Representation, BFS, DFS
Algorithms and data structures
GRAPHS G=<V,E> Adjacent vertices Undirected graph
CSE 417: Algorithms and Computational Complexity
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:

#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching for paths in graphs

#2© K.Goczyła Definitions Graph: G =  V, E , where V is a set of vertices, E is a set of edges (arcs). E  V  V for directed graph (set od arcs) E  { {x, y}: x, y  V  x  y} for undirected graph (set of edges) |V| = n, |E| = m Undirected graph:Directed graph: Degree of a vertex: the number of direct neighbors. The two exemplary graphs are connected.

#3© K.Goczyła Computer representations Adjacency matrix Incidence lists 1: 2,3 2: 3: 2,4 4: 5: 4,6 6: 5 1: 2,3 2: 3: 2,4 4: 5: 4,6 6: 5 1: 2,3,5 2: 1,3,4 3: 1,2,5 4: 2,5,6 5: 1,3,4,6 6: 4,5 1: 2,3,5 2: 1,3,4 3: 1,2,5 4: 2,5,6 5: 1,3,4,6 6: 4,5

#4© K.Goczyła Traversing graphs: Depth-First Search Incidence lists 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: 6 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: STACK: 1, 2, 4, 6, 5, 8, 9, 7, 3, 13, 12 nnnnnnnnnnnnnnnnnnnnnnnnnn / / / / / / / / / // / / / / ///// /, 10, 11 /// //

#5© K.Goczyła Recursive procedure (an outline): /* IL – incidence lists, NEW – table of visit tags */ Procedure DFSearch (in v: node) /* traverse the graph by DFS, starting from vertex v */ { visit(v); NEW[v] = false; /* mark that the vertex v has already been visited */ for u  IL[v] do if NEW[u] then DFSearch(u) } /* vertex v has been visited*/ /* main program */ /* input: incidence lists IL */ { for v  V do NEW[v] = true; /* initialize visit tags */ for v  V do if NEW[v] then DFSearch(v) } /* IL – incidence lists, NEW – table of visit tags */ Procedure DFSearch (in v: node) /* traverse the graph by DFS, starting from vertex v */ { visit(v); NEW[v] = false; /* mark that the vertex v has already been visited */ for u  IL[v] do if NEW[u] then DFSearch(u) } /* vertex v has been visited*/ /* main program */ /* input: incidence lists IL */ { for v  V do NEW[v] = true; /* initialize visit tags */ for v  V do if NEW[v] then DFSearch(v) } Computational complexity: O(m+n) Traversing graphs: Depth-First Search

#6© K.Goczyła Incidence lists 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: 6 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: QUEUE: 1, 2, 4, 6, 5, 8, 9, 7, 3, 13, 12 nnnnnnnnnnnnnnnnnnnnnnnnnn / / / / / / / / / // / / / / ///// /, 10, 11 /// // 1 Path Traversing graphs: Breadth-First Search Paths: to 2: 1, 2; to 3: 1,4,7,3 to 4: 1,4; to 5: 1,4,6,5 to 6: 1,4,6; to 7: 1,4,7; to 8: 1,4,6,5,8 to 9: 1,4,6,9 to 10: 1,12,10; to 11: 1,12,11; to 12: 1,12; to 13: 1,4,6,13

#7© K.Goczyła /* IL – incidence lists, NEW – table of visit marks, PATH – path of graph search */ Procedure BFSearch (in v: node) /* traverse the graph by BFS, starting from vertex v */ { QUEUE =  ; QUEUE = v; /* initialize the queue */ NEW[v] = false; /* mark that the vertex v has already been visited */ while QUEUE !=  do { p = QUEUE; visit(p); for u  IL[p] do if NEW[u] then { QUEUE = u; NEW = false; PATH[u] = p } /* IL – incidence lists, NEW – table of visit marks, PATH – path of graph search */ Procedure BFSearch (in v: node) /* traverse the graph by BFS, starting from vertex v */ { QUEUE =  ; QUEUE = v; /* initialize the queue */ NEW[v] = false; /* mark that the vertex v has already been visited */ while QUEUE !=  do { p = QUEUE; visit(p); for u  IL[p] do if NEW[u] then { QUEUE = u; NEW = false; PATH[u] = p } Computational complexity: O(m+n) Iterative procedure (an outline): Traversing graphs: Breadth-First Search

#8© K.Goczyła Finding shortest paths - Ford-Bellman algorithm Assumptions: For any arc  u, v   V of a directed graph there exists a number a(u, v) called the weigth of the arc. In the graph there are no cycles of negative length (sum of weigths). If there is no arc from u to v, we assume a(u, v) = . 0 1   3 / k D[1] D[2] D[3] D[4] D[5] s= (3) (1) (4) (8) (1)(2) (3) (-5) 1 Matrix of weigths  1   3      1 -5   2      4   1   3      1 -5   2      4  Paths: to 2: 1, to 3: 1, 2, 3 to 5: 1, 2, 3, 5 to 4: 1, 2, 3, 5, 4 no change

#9© K.Goczyła Procedure (an outline): Input: A – matrix of arc weigths for a directed graph with no cycles of negative length s – starting vertex (source) Output: D – distances from the source to all vertices of the graph P – lists of paths from the source to all vertices of the graph Procedure Ford-Bellman (in s: node) {for v  V do D[v] = A[s,v]; D[s] = 0; /* initialization */ for k = 1 to n-2 do for v  V–{s} do for u  V do { D[v]= min(D[v], D[u]+A[u,v]); if there was a change, then store the previous vertex on the path } Input: A – matrix of arc weigths for a directed graph with no cycles of negative length s – starting vertex (source) Output: D – distances from the source to all vertices of the graph P – lists of paths from the source to all vertices of the graph Procedure Ford-Bellman (in s: node) {for v  V do D[v] = A[s,v]; D[s] = 0; /* initialization */ for k = 1 to n-2 do for v  V–{s} do for u  V do { D[v]= min(D[v], D[u]+A[u,v]); if there was a change, then store the previous vertex on the path } Computational complexity: O(n 3 ) Finding shortest paths - Ford-Bellman algorithm

#10© K.Goczyła 1 Assumptions: For any arc  u, v   V of a directed graph there exists a non-negative number a(u, v) called the weigth of the arc. For an undirected graph we assume a(u, v) = a(v, u). If there is no arc from u to v, we assume a(u, v) = . 0 1     6 3  (1) (2) (1) (5) (7) (4) (3) (1) / / / D[1] D[2] D[3] D[4] D[5] D[6] Paths T = { 2, 3, 4, 5, 6 } /// / / Finding shortest paths - Dijkstra algorithm

#11© K.Goczyła Procedure (an outline): Input: A – matrix of non-negative arc weigths, s – starting vertex (source) Output: D – matrix of distances from the source to all vertices of the graph P – lists of paths from the source to all vertices of the graph Procedure Dijkstra (in s: node) {for v  V do D[v] = A[s,v]; D[s] = 0; /* initialization */ T = V–{s}; while T !=  do { u = any vertex r  T such that D[r] is minimal; Store the path basing on the path to the previous vertex; T = T–{u}; for v  T do { D[v]= min(D[v], D[u]+A[u,v]); if there was a change, then store the previous vertex on the path } Input: A – matrix of non-negative arc weigths, s – starting vertex (source) Output: D – matrix of distances from the source to all vertices of the graph P – lists of paths from the source to all vertices of the graph Procedure Dijkstra (in s: node) {for v  V do D[v] = A[s,v]; D[s] = 0; /* initialization */ T = V–{s}; while T !=  do { u = any vertex r  T such that D[r] is minimal; Store the path basing on the path to the previous vertex; T = T–{u}; for v  T do { D[v]= min(D[v], D[u]+A[u,v]); if there was a change, then store the previous vertex on the path } Computation complexity: O(m log n) – after some optimization Finding shortest paths - Dijkstra algorithm Note: The order of computation complexity does not change if we find the shortest path only to one specific vertex.

#12© K.Goczyła Finding Euler’s path in a graph Euler’s path: any path that traverses each edge of the graph exactly once. Euler’s cycle: Euler’s path where the starting and ending vertices are the same. Theorem: An Euler’s path in a graph exists if and only if the graph is connected and contains no more than 2 vertices of odd degree. These vertices are the endpoints of the Euler’s path. „Closed envelope” – no Euler’s path „Open envelope” has an Euler’s path Note: If a connected graph does not have vertices of odd degree, then each Euler’s path is a cycle.

#13© K.Goczyła Finding an Euler’s cycle in a graph with no vertices of odd degree Incidence lists 1 : 2, 3 2 : 1, 3, 7, 8 3 : 1, 2, 4, 5 4 : 3, 5 5 : 3, 4, 6, 8 6 : 5, 7, 8, 9, 7 : 2, 6, 8, 9 8 : 2, 5, 6, 7 9 : 6, 7 1 : 2, 3 2 : 1, 3, 7, 8 3 : 1, 2, 4, 5 4 : 3, 5 5 : 3, 4, 6, 8 6 : 5, 7, 8, 9, 7 : 2, 6, 8, 9 8 : 2, 5, 6, 7 9 : 6, STACK: 1, 2, 3, 1, 4, 5, 3, 6, 7, 2, 8 / / 5 2 EC: x // x x / / / x /// / / / //, 5, 6, 9, 7, 8 1, 3, 5, 8, 7, 9, 6, 8, 2, 7, 6, 5, 4, 3, 2, 1 x / / / / x x x x x x / / // // /// / / / x x x / // x ////// /// ///

#14© K.Goczyła Procedure (an outline): Input: IL – incidence lists of a connected graph with no vertices of odd degree, Output: EC – Euler’s cycle in the graph Procedure Euler { STACK =  ; EC =  ; /* initialization */ v = any vertex; STACK <- v; /* push v */ while STACK !=  do if IL[v] !=  then { u = first node from list IL[v]; STACK <- u; /* push u */ IL[v]=IL[v]–{u}; IL[u]=IL[u]-{v}; /* remove edge {v,u} from the graph */ v = u } else/* IL[v] is already empty */ { v <- STACK;/* pop v */ CE <- v/* append v to Euler’s cycle */ } Input: IL – incidence lists of a connected graph with no vertices of odd degree, Output: EC – Euler’s cycle in the graph Procedure Euler { STACK =  ; EC =  ; /* initialization */ v = any vertex; STACK <- v; /* push v */ while STACK !=  do if IL[v] !=  then { u = first node from list IL[v]; STACK <- u; /* push u */ IL[v]=IL[v]–{u}; IL[u]=IL[u]-{v}; /* remove edge {v,u} from the graph */ v = u } else/* IL[v] is already empty */ { v <- STACK;/* pop v */ CE <- v/* append v to Euler’s cycle */ } Computational complexity: O(m) Finding an Euler’s cycle

#15© K.Goczyła EC: 1, 6, 4, 5, 2, 4, 3, 2, 1 1. We add an edge between the two vertices of odd degree. 2. We apply the Euler procedure: 3. We change the Euler’s cycle into the Euler’s path: EP: 4, 3, 2, 1, 6, 4, 5, 2 2, 5, 4, 6, 1, 2, 3, 4 or: Finding an Euler’s path in a graph with 2 vertices of odd degree

#16© K.Goczyła Finding a Hamilton’s path in a graph Hamilton’s path: any path that visits each vertex of a graph exactly once. Hamilton’s cycle: a Hamilton’s path where the starting and ending vertices are the same. Finding a Hamilton’s path/cycle is computationally hard – there is no known algorithm that finds a Hamilton’s path in a time that is polynomially dependent on the number of vertices n. Graph that has a Hamilton’s pathGraph with no Hamilton’s path Searching for a shortest Hamilton’s cycle in a weighted graph is called Travelling salesman problem