10/9/2014 Ma/CS 6a Class 6: Introduction to Graphs By Adam Sheffer Todayβs Class is about Six Degrees of Kevin Bacon 1 10/9/2014 Six Degrees of Kevin Bacon Claim. Any actor can be linked through his/her film roles to Kevin Bacon within six steps. ο Example. Keanu Reeves: ο Graphs Undirected graph π Edge Directed graph π π π Vertex 2 10/9/2014 Graph Representation ο We write πΊ = π, πΈ . That is, the graph πΊ has vertex set π and edge set πΈ. ο Example. In the figure: π β¦ π = π, π, π, π, π . β¦ πΈ = * π, π , π, π , π, π , π, π , π, π +. π π π π Graph Representation (cont.) π = π, π, π, π, π . ο πΈ = * π, π , π, π , π, π , π, π , π, π +. ο π π π π π π π π π π 3 10/9/2014 What is this Good For? Cold war analysis Communication networks Social networks Robot Motion Planning Paths and Cycles π π Path between π and π. π Cycle through π A cycle is a path that starts and ends in the same vertex. 4 10/9/2014 More on Paths and Cycles ο A path/cycle is said to be simple if it does not visit any vertex more than once. ο The length of a path/cycle is the number of edges that it consists of. ο Example. A simple cycle of length 5. Connectedness and Degrees A graph is connected if there is a path between any two vertices. ο The degree of a vertex is the number of edges that are adjacent to it. ο οΌ Connected? Connected? deg(π) = 2 deg(π) = 1 π π 5 10/9/2014 Simple Graphs An edge is a loop if both of its endpoints are the same vertex. ο Two edges are parallel if they are between the same pair of vertices. ο A graph is simple if it contains no loops and no parallel edges. ο For now, we only consider simple graphs. ο Parallel edges A loop Warm-up Exercise ο Prove. In any graph, the sum of the degrees of the vertices is even. ο Proof. Every edge contributes 1 to the degree of exactly two vertices. Thus, deg π£ = π£βπ 2=2πΈ . πβπΈ 6 10/9/2014 Paths and Degrees ο Problem. Let πΊ = π, πΈ be a graph such that the degree of every π£ β π is at least π (for some π β₯ 2). Prove that πΊ contains a path of length π. A graph with minimum degree 3. Proof Assume, for contradiction, that a longest path π is of length π < π. ο Consider a vertex π£ which is an endpoint of π. ο Since deg π£ β₯ π β₯ π + 1, it must be connected to at least one vertex π’ β π. ο By adding the edge π£, π’ to π, we obtain a longer path, contradicting the maximality of π. ο π£ 7 10/9/2014 A Variant of the Problem ο Problem. Let πΊ = π, πΈ be a graph such that the degree of every π£ β π is at least π (for some π β₯ 2). β¦ What is the minimum length of a cycle in πΊ? π+1 Connectivity Problem ο Prove. The vertices of a connected graph πΊ can always be ordered as π£1 , π£2 , β¦ , π£π such that for every 1 < π β€ π, if we remove π£π , π£π+1 , β¦ , π£π and the edges adjacent to these vertices, πΊ remains connected. π£4 π£3 π£2 π£1 π£5 8 10/9/2014 Proof Pick any vertex as π£1 . ο Pick a vertex that is connected to π£1 in πΊ and set it as π£2 . ο Pick a vertex that is connected either to π£1 or to π£2 in πΊ and set it as π£3 . οβ¦ ο Back to Bacon We wish to build a graph for the problem. ο What are the vertices of the graph? ο β¦ A vertex for each actor. ο When is there an edge between two vertices? β¦ When the corresponding actors played in a common movie. ο Is the graph directed? β¦ No. 9 10/9/2014 Example ο We want to check if every actor has a finite Bacon Number. What should we check? β¦ Whether the graph is connected. Bacon Numbers The Bacon number of an actor is the minimum number of steps required to connect him to Kevin Bacon. ο Example. By the picture below: ο β¦ Christopher Walkenβs Bacon number is 2. β¦ Keanu Reevesβ Bacon number is 4. 10 10/9/2014 Bacon Numbers (2) ο ο But Keanu Reevesβ Bacon number is 2! What should we do in the graph to find the correct Bacon number of an actor? β¦ The length of the shortest path from the actorβs vertex to Baconβs vertex. The BFS Algorithm ο We wish to find out: β¦ Whether the graph is connected. β¦ The shortest paths from Baconβs vertex to every other vertex. ο The BFS algorithm: β¦ Input. An undirected graph πΊ = (π, πΈ) and a vertex π β π. β¦ Output. A shortest path from π to any other vertex of πΊ (if such a path exists). β¦ πΊ is connected if and only if there is a path to every vertex. 11 10/9/2014 Trees and Forests In an undirected graph, a tree is a connected subgraph containing no circles. ο A forest is a set of non-connected trees. ο A forest with four trees Rooted Trees A rooted tree is a tree with a special vertex β the root β that is singled out. ο We draw the tree with the root on top, and the edges βgrow downwardsβ. ο A vertex π£ is the parent of a vertex π’ if there is an edge π’, π£ and π£ is above π’. ο β¦ Each vertex, except for the root, has a unique parent. π π‘ π is the root and π‘βs parent 12 10/9/2014 BFS Output π π π π π π π π π π π π ο The output is a BFS tree, containing only shortest paths from π . β¦ A rooted tree with root π . Test Your Intuition ο Problem. Is there always a unique tree containing the shortest paths from π ? ο Answer. No! π π π π π π π π π π π π 13 10/9/2014 ErdΕs Numbers: the Math Version ο Paul ErdΕs (1913-1996). A Hungarian mathematician. Possibly the most prolific mathematician ever. β¦ People that wrote a paper with ErdΕs have an ErdΕs number of 1. β¦ People that wrote a paper with someone that has an ErdΕs number of 1, have an ErdΕs number of 2. β¦ Etc. ο Most leading scientists have a small ErdΕs number. BFS: Colors We call the vertex π that we start from the root of the tree. ο BFS scans the graph starting from the root. ο During the scan, every vertex has a color: ο β¦ Vertices that the algorithm did not visit yet are colored white. β¦ Vertices that the algorithm did visit, but is not yet done with are colored gray. β¦ Vertices that the algorithm is done with are colored black. 14 10/9/2014 Data Structure: Queue A queue stores βobjectsβ (in our case β vertices). ο Supports the operations: ο β¦ Enqueue β insert an object to the back of the queue. β¦ Dequeue β remove an object from the front of the queue. BFS: The Main Idea A queue π holds the vertices that are currently gray. At first π = π . ο At each step, take out a vertex π’ β π and for every edge π adjacent to π’: ο β¦ If the other vertex of π is gray or black, do nothing. β¦ If the other vertex of π is white, color it gray and insert it into π. ο After going over all of π’βs edges, color π’ black, and move to the next vertex in π. 15 10/9/2014 Example: BFS Run s s b f s b f e s b f e c e c s c d s b f b f s b f e c d d s e c e c d d b f e c d b f d e c d Example: Another BFS Run 16 10/9/2014 The End 17
© Copyright 2024