Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 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)

2 Graphs Definitions Examples The Graph ADT BDL

3 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

4 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)}

5 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)}

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

7 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

8 Circuit Analysis and Layout

9 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

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

11 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}

12 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

13 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

14 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

15 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

16 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

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

18 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

19 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

20 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).

21 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

22 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

23 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

24 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.

25 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.

26 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.

27 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.

28 Data Structures for Graphs
Edge list Adjacency lists Adjacency matrix

29 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

30 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

31 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)

32 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)

33 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))

34 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 )

35 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

36 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!

37 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 )

38 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

39 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

40 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

41 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.

42 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.

43 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

44 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

45 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

46 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

47 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

48 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

49 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’

50 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.

51 BFS Example Starting node for traversal The nodes in level 1

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

53 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

54 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.

55 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.


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google