Introduction to Programming

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

PSU CS Algorithms Analysis and Design Graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
CSC 213 ORD DFW SFO LAX Lecture 20: Graphs.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
B.Ramamurthy1 Graphs Chapter 12 B.Ramamurthy. 2 Introduction A structure that represents connectivity information. A tree is kind of graph. Applications.
Graphs.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive.
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
Graphs – ADTs and Implementations ORD DFW SFO LAX
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,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
Graphs1 Definitions Examples The Graph ADT LAX PVD LAX DFW FTL STL HNL.
Chapter 6 Graphs ORD DFW SFO LAX
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
1 Data Structures for Graphs Edge list Adjacency lists Adjacency matrix.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
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.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
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.
Chapter 05 Introduction to Graph And Search Algorithms.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs Part 1.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs.
Graphs.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs CSE 2011 Winter November 2018.
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search Graph Traversals Depth-First Search DFS.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW /15/ :57 AM
Graphs.
Graphs.
Graphs.
Graphs.
Presentation transcript:

Introduction to Programming Computer Science 187 Introduction to Programming with Data Structures Lecture 24 Graphs - Part 1 Announcements Final exam is on December 20 at 10:30 (ELAB303). Closed book, no notes; same general format as the midterm. Two OWLs are available Programming project 6 is available (last one)

Graphs Definitions Examples The Graph ADT BDL

Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge Bristol Southhampton Dover 60 140 190 150 100 120 110 An undirected graph start fill pan with water take egg from fridge break egg into pan boil water add salt to water A directed graph

What is a Graph? A graph G = (V,E) is composed of: V: set of vertices or nodes E: set of edges connecting the vertices (or nodes) in V An edge e = (u,v) is a pair of vertices Example: V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}

Graphs, again. Graphs are a formalism useful for representing relationships between things a graph G is represented as G = (V, E) V is a set of vertices: {v1, v2, …, vn} E is a set of edges: {e1, e2, …, em} where each ei connects two vertices (vi1, vi2) operations include: iterating over vertices iterating over edges iterating over vertices adjacent to a specific vertex asking whether an edge exists connected two vertices Han Luke Leia V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)}

Applications Electronic Circuits: find the path of least resistance to CS187 CS187 Networks: roads, flights, communications BDL

Graph Applications Storing things that are graphs by nature Compilers distance between cities, rooms in Clue airline flights, travel options relationships between people, things Designing optimal connections on a computer chip Doing similarity testing (e.g. for a dating service) Pert charts Playing games Compilers callgraph - which functions call which others dependence graphs - which variables are defined and used at which statements

Circuit Analysis and Layout

S deg(v) = 2 * number of edges Graph Terminology adjacent vertices: connected by an edge degree (of a vertex): # of adjacent vertices S deg(v) = 2 * number of edges v in V Because adjacent vertices each count the adjoining edge, it will be counted twice

Paths Path: a sequence of vertices v1 , v2 , … vk such that consecutive vertices vi and vi+1 are adjacent.

Paths, take 2 A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1)  E for all 0  i < n. Chicago Seattle Salt Lake City San Francisco Dallas p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

Cycles Simple Path: no repeated vertices Cycle: simple path except that the last vertex is the same as the first. b e c a c d a

Connections and Subgraphs Connected graph: any two vertices are connected by some path. Subgraph: a subset of the edges and vertices in G Connected components: maximal connected subgraph. Three connected components

Connected Components A subset of the nodes of the graph is a connected component (or just a component) if there is a path from every node in the subset to every other node in the subset Three connected components

Directed vs. Undirected Graphs A directed graph is one in which the edges have a direction An undirected graph is one in which the edges do not have a direction Note: Whether a graph is directed or undirected is a logical distinction—it describes how we think about the graph Depending on the implementation, we may or may not be able to follow a directed edge in the “backwards” direction For directed graphs, If a directed edge goes from node S to node D, we call S the source and D the destination of the edge The edge is an out-edge of S and an in-edge of D S is a predecessor of D, and D is a successor of S The in-degree of a node is the number of in-edges it has The out-degree of a node is the number of out-edges it has

Connectedness in Directed Graphs An undirected graph is connected if there is a path from every node to every other node A directed graph is strongly connected if there is a path from every node to every other node A directed graph is weakly connected if the underlying undirected graph is connected

Trees and Forests (free) tree: connected graph without cycles Forest: collections of trees

Complete Graph n = 5 m= (5 * 4)/2 = 10 Let n = #vertices and m = #edges complete graph - all pairs of vertices are adjacent m= (1/2) S deg(v) = (1/2) S (n - 1) = n(n-1)/2 Each of the n vertices is incident on n - 1 vertices, however, we would have counted each edge twice!!! Therefore, intuitively, m = n(n-1)/2. Therefore, if a graph is not complete, m < n(n-1)/2 vÎV vÎV n = 5 m= (5 * 4)/2 = 10

More Connectivity n = #vertices and m = #edges For a tree m = n - 1 If m < n - 1, G is not connected n = 5 m = 4 n = 5 m = 3

Spanning Tree A spanning tree of G is a subgraph which - is a tree - contains all vertices of G Removal of any edge in the spanning tree disconnects the tree into disjoint trees - least fault tolerant (see next slide).

AT&T vs. AT&T (Al T&T) Al wants to call Anders, Giyasettin and the graders to suggest an extension for the next program… One fault will disconnect part of graph!! A cycle would be more fault tolerant and only requires n edges Grader Grader Anders Grader Giyasettin But physical plant cuts the wire

Euler and the Bridges of Konigsberg Can one walk across each bridge exactly once and return at the starting point? Suppose you were a UPS driver, and you didn’t want to retrace your steps. In 1736, Euler proved that this is not possible

Graph Model of Konigsberg (with parallel edges) Eulerian Tour: path that traverses every edge exactly once and returns to the first vertex Euler’s Theorem: A graph has a Eulerian Tour if and only if all vertices have even degree

The Graph ADT The Graph ADT is a positional container whose positions are the vertices and the edges of the graph. - size() Return the number of vertices plus the number of edges of G. - isEmpty() - elements() - positions() - swap() - replaceElement() Notation: Graph G; Vertices v, w; Edge e; Object o - numVertices() Return the number of vertices of G. - numEdges() Return the number of edges of G. - vertices() Return an enumeration of the vertices of G. - edges() Return an enumeration of the edges of G.

Graph ADT, cont'd. directedEdges() Return an enumeration of all directed edges in G. undirectedEdges() Return an enumeration of all undirected edges in G. incidentEdges(v) Return an enumeration of all edges incident on v. inIncidentEdges(v) Return an enumeration of all the incoming edges to v. outIncidentEdges(v) Return an enumeration of all outgoing edges from v. opposite(v, e) Return an endpoint of e distinct from v degree(v) Return the degree of v. inDegree(v) Return the in-degree of v. outDegree(v) Return the out-degree of v.

More Methods adjacentVertices(v) Return an enumeration of the vertices adjacent to v. inAdjacentVertices(v) Return an enumeration of the vertices adjacent to v along incoming edges. outAdjacentVertices(v) Return an enumeration of the vertices adjacent to v along outgoing edges. areAdjacent(v,w) Return whether vertices v and w are adjacent. endVertices(e) Return an array of size 2 storing the end vertices of e. origin(e) Return the end vertex from which e leaves. destination(e) Return the end vertex at which e arrives. isDirected(e) Return true iff e is directed.

Update Methods makeUndirected(e) Set e to be an undirected edge. reverseDirection(e) Switch the origin and destination vertices of e. setDirectionFrom(e, v) Sets the direction of e away from v, one of its end vertices. setDirectionTo(e, v) Sets the direction of e toward v, one of its insertEdge(v, w, o) Insert and return an undirected edge between v and w, storing o at this position. insertDirectedEdge(v, w, o) Insert and return a directed edge between v insertVertex(o) Insert and return a new (isolated) vertex storing o at this position. removeEdge(e) Remove edge e.

Data Structures for Graphs Edge list Adjacency lists Adjacency matrix

Data Structures for Graphs A Graph! How can we represent it? To start with, we store the vertices and the edges into two containers, such that each edge object has references to the vertices it connects. Additional structures can be used to efficiently perform the methods of the Graph ADT

Edge List The edge list structure simply stores the vertices and the edges in unsorted sequences. Easy to implement. Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence

Performance of the Edge List Operation Time size, isEmpty, replaceElement, swap O(1) numVertices, numEdges O(1) vertices O(n) edges, directedEdges, undirectedEdges O(m) elements, positions O(n+m) endVertices, opposite, origin, destination, isDirected O(1) incidentEdges, inIncidentEdges, outIncidentEdges, adjacentVertices, inAdjacentVertices, O(m) outAdjacentVertices, areAdjacent, degree, inDegree, outDegree insertVertex, insertEdge, insertDirectedEdge, O(1) removeEdge, makeUndirected, reverseDirection, setDirectionFrom, setDirectionTo removeVertex O(m)

Adjacency List (traditional method) adjacency list of v: sequence of vertices adjacent to v represent the graph by the adjacency lists of all the vertices Space = O(N + S deg(v)) = O(N + M)

Performance of the Adjacency List Representation Operation Time size, isEmpty, replaceElement, swap O(1) numVertices, numEdges O(1) vertices O(n) edges, directedEdges, undirectedEdges O(m) elements, positions O(n+m) endVertices, opposite, origin, destination, O(1) isDirected, degree, inDegree, out-Degree incidentEdges(v), inIncidentEdges(v), outIncidentEdges(v), adjacentVerti-ces(v), O(deg(v)) inAdjacentVertices(v), outAdja-centVertices(v) areAdjacent(u, v) O(min(deg(u), deg(v))) insertVertex, insertEdge, insertDirected-Edge, O(1) removeEdge, makeUndirected, reverseDirection removeVertex(v) O(deg(v))

Adjacency Matrix (traditional) matrix M with entries for all pairs of vertices M[i,j] = true means that there is an edge (i,j) in the graph. M[i,j] = false means that there is no edge (i,j) in the graph. There is an entry for every possible edge, therefore: Space = O(N 2 )

Adjacency Matrix (modern) The adjacency matrix structure augments the edge list structure with a matrix where each row and column corresponds to a vertex. BOS DFW JFK LAX MIA ORD SFO BOS DFW JFK LAX MIA ORD SFO

Adjacency-matrix representation II An adjacency matrix can equally well be used for digraphs (directed graphs) A 2-D array has a mark at [i][j] if there is an edge from node i to node j A B G E F D C A B C D E F G A B C D E F G Again, this is only suitable for small graphs!

Performance of the Adjacency Matrix Representation Operation Time size, isEmpty, replaceElement, swap O(1) numVertices, numEdges O(1) vertices O(n) edges, directedEdges, undirectedEdges O(m) elements, positions O(n+m) endVertices, opposite, origin, destination, O(1) isDirected, degree, inDegree, outDegree incidentEdges, inIncidentEdges, outIncidentEdges, O(n) adjacentVertices, inAdja-centVertices, outAdjacentVertices, areAdjacent O(1) insertEdge, insertDirectedEdge, removeEdge, makeUndirected, reverseDirection, O(1) setDirectionFrom, setDirectionTo insertVertex, removeVertex O(n 2 )

Graph Algorithms Undirected Graph Directed Graph Weighted Graphs Depth-first Search Breadth-first Search Directed Graph Search: DFS, BFS Transitive Closure Weighted Graphs Shortest Path Minimum Spanning Tree

The difference is that a graph may have cycles Searching a graph With certain modifications, any tree search technique can be applied to a graph This includes depth-first, breadth-first, depth-first iterative deepening, and other types of searches The difference is that a graph may have cycles We don’t want to search around and around in a cycle To avoid getting caught in a cycle, we must keep track of which nodes we have already explored There are two basic techniques for this: Keep a set of already explored nodes, or Mark the node itself as having been explored

Graph Traversals: Depth First Search Search as far as we can along a single path before backtracking Analogous to a depth first search in a tree

Exploring a Labyrinth A depth-first search (DFS) in an undirected graph G is like wandering in a labyrinth with a string and a can of red paint without getting lost. We start at vertex s, tying the end of our string to the point and painting s “visited”. Next we label s as our current vertex called u. Now we travel along an arbitrary edge (u,v). If edge (u,v) leads us to an already visited vertex v we return to u; (u,v) is called a back edge. If edge (u,v) leads us to an unvisited vertex, we unroll our string and move to v, paint v “visited”, set v as our current vertex. Edge (u,v) is called a discovery edge. Repeat the previous steps.

Exploring a Labyrinth, cont. Eventually, we will get to a point where all incident edges on u lead to visited vertices. We then backtrack by unrolling our string to a previously visited vertex v. Then v becomes our current vertex and we repeat the previous steps. Then, if all incident edges on v lead to visited vertices, we backtrack as we did before. We continue to backtrack along the path we have traveled, finding and exploring unexplored edges, and repeating the procedure. When we backtrack to vertex s and there are no more unexplored edges incident on s, we have finished our DFS search.

Depth First Search PseudoCode Algorithm DFS(v); Input: A vertex v in a graph Output: A labeling of the edges as “discovery” edges and “backedges” for each edge e incident on v do if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge recursively call DFS(w) else label e as a backedge

Determining Incident Edges DFS depends on how you obtain the incident edges. If we start at A and we examine the edge to F, then to B, then E, C, and finally G If we instead examine the tree starting at A and looking at G, then C, then E, B, and finally F, Resulting set of backEdges, discoveryEdges and recursion points is different. Now an example of a DFS. discoveryEdge backEdge return from dead end

Example Depth First Search ‘Back’ Edge ‘Discovery’ Edge S= E F G H I J K L M N O P A B C D E F G H I J K L M N O P A B C D Input Graph and Starting Vertex Path of discovery edges traces from A until back edge (B,A) is hit

DFS, cont. Reaching F, which is a dead end. J K L M N O P A B C D E F G H I J K L M N O P A B C D Reaching F, which is a dead end. After backtracking to vertex C, resuming with edge (C,G) and hitting another dead end at J

DFS, cont. E F G H I J K L M N O P A B C D A B C D E F G H I J K L M N O P After backtracking to G and hitting three more dead ends at D, L, and K After backtracking to N and hitting the visited edge (N,I). DONE. Search Order: A, E, I, M, N, K, O, P, L, H, D, C, B, F, G, J

Depth First Search Visits all vertices in the connected component that contains s (the start vertex). The discovery edges form a spanning tree of the connected component containing s. Can be performed in O(n+m) time. Based on DFS, we can defined O(n+m) algorithms for the following problems: Test whether G is connected Compute a spanning tree of G, if G is connected Compute the connected components of G Compute a path between two given vertices of G, or reporting that no such path exists Compute a cycle in G or reporting that G has no cycles

Depth First Search Pseudo-Code Algorithm DFS(G,v): Input: A graph G and a vertex v of G Output: A labeling of the edges as discovery edges and back edges. for all edges e in G.incidentEdges(v) do if edge e is unexplored then w <- G.opposite(v,e) recursively call DFS(G,w) else label e as a back edge the ‘string’ the ‘paint’

Breadth-First Search Like DFS, a Breadth-First Search (BFS) traverses a connected component of a graph, and in doing so defines a spanning tree with several useful properties The starting vertex s has level 0, and, as in DFS, defines that point as an “anchor.” In the first round, the string is unrolled the length of one edge, and all of the edges that are only one edge away from the anchor are visited. These edges are placed into level 1 In the second round, all the new edges that can be reached by unrolling the string 2 edges are visited and placed in level 2. This continues until every vertex has been assigned a level. The label of any vertex v corresponds to the length of the shortest path from s to v.

BFS Example Starting node for traversal The nodes in level 1

BFS Example, cont. The nodes in level 4 The nodes in level 5

BFS Pseudo-Code Could use a queue Algorithm BFS(s): Input: A vertex s in a graph Output: A labeling of the edges as “discovery” edges and “cross edges” initialize container L 0 to contain vertex s i ¬ 0 while L i is not empty do create container L i+1 to initially be empty for each vertex v in L i do if edge e incident on v do let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge insert w into L i+1 else label e as a cross edge i ¬ i + 1 Could use a queue

Properties of the Breadth First Search Let G be an undirected graph on which a BFS traversal starting at vertex s has been performed. Then The traversal visits all vertices in the connected component of s. The discovery-edges form a spanning tree T, which we call the BFS tree, of the connected component of s For each vertex v at level i, the path of the BFS tree T between s and v has i edges, and any other path of G between s and v has at least i edges. If (u, v) is an edge that is not in the BFS tree, then the level numbers of u and v differ by at most one.

Properties of the Breadth First Search Let G be a graph with n vertices and m edges. A BFS traversal of G takes time O(n + m). Also, there exist O(n + m) time algorithms based on BFS for the following problems: Testing whether G is connected. Computing a spanning tree of G Computing the connected components of G Computing, for every vertex v of G, the minimum number of edges of any path between s and v.