GRAPHS Lecture 17 CS2110.

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science for Some.
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CompSci 102 Discrete Math for Computer Science April 19, 2012 Prof. Rodger Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graph & BFS.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
9.2 Graph Terminology and Special Types Graphs
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
1 CS104 : Discrete Structures Chapter V Graph Theory.
GRAPHS Lecture 19 CS2110 – Fall Time to do A4, Recursion Histogram: max: [00:02): 17 av: 5.2 [02:04): 102 median: 4.5 [04:06): 134 [06:08):
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.
Data Structures & Algorithms Graphs
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Great Theoretical Ideas in Computer Science for Some.
COMPSCI 102 Introduction to Discrete Mathematics.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Graphs - I CS 2110, Spring 2017 Leonhard Euler. 1736
Graphs Lecture 19 CS2110 – Spring 2013.
School of Computing Clemson University Fall, 2012
Lecture 11 Graph Algorithms
GRAPHS Lecture 16 CS2110 Fall 2017.
CSC317 Graph algorithms Why bother?
Graph theory Definitions Trees, cycles, directed graphs.
Depth-First Search.
CS120 Graphs.
Graph Algorithm.
Planarity Testing.
Graphs Lecture 18 CS2110 – Fall 2009.
Modeling and Simulation NETW 707
Graphs Chapter 13.
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
Richard Anderson Autumn 2016 Lecture 5
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
Graph Algorithms Lecture 19 CS 2110 — Spring 2019.
Richard Anderson Winter 2009 Lecture 6
Text Book: Introduction to algorithms By C L R S
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS Lecture 17 CS2110 Spring 2018.
Discrete Mathematics for Computer Science
GRAPHS Lecture 17 CS 2110 — Spring 2019.
Introduction to Graph Theory
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Minimum Spanning Trees
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

GRAPHS Lecture 17 CS2110

Announcements A5 Heaps Due October 27 Prelim 2 in ~3 weeks: Thursday Nov 15 A4 being graded right now Mid-Semester College Transitions Survey on Piazza

These aren't the graphs we're looking for

Graphs A graph is a data structure A graph has: a set of vertices a set of edges between vertices Graphs are a generalization of trees

This is a graph

This is a graph

A Social Network Graph

Viewing the map of states as a graph http://www.cs.cmu.edu/~bryant/boolean/maps.html Each state is a point on the graph, and neighboring states are connected by an edge. Do the same thing for a map of the world showing countries

Graphs K5 K3,3

Undirected graphs A undirected graph is a pair (V, E) where V is a (finite) set E is a set of pairs (u, v) where u,v  V Often require u ≠ v (i.e., no self-loops) Element of V is called a vertex or node Element of E is called an edge or arc |V| = size of V, often denoted by n |E| = size of E, often denoted by m A B C D E V = {A, B, C, D, E} E = {(A, B), (A, C), (B, C), (C, D)} |V| = 5 |E| = 4

Directed graphs A directed graph (digraph) is a lot like an undirected graph V is a (finite) set E is a set of ordered pairs (u, v) where u,v  V A B C D E Every undirected graph can be easily converted to an equivalent directed graph via a simple transformation: Replace every undirected edge with two directed edges in opposite directions … but not vice versa V = {A, B, C, D, E} E = {(A, C), (B, A), (B, C), (C, D), (D, C)} |V| = 5 |E| = 5

Graph terminology Vertices u and v are called the source and sink of the directed edge (u, v), respectively the endpoints of (u, v) or {u, v} Two vertices are adjacent if they are connected by an edge The outdegree of a vertex u in a directed graph is the number of edges for which u is the source The indegree of a vertex v in a directed graph is the number of edges for which v is the sink The degree of a vertex u in an undirected graph is the number of edges of which u is an endpoint A B C D E 1 2 A B C D E 2

More graph terminology A path is a sequence v0,v1,v2,...,vp of vertices such that for 0 ≤ i < p, (vi, vi+1)∈E if the graph is directed {vi, vi+1}∈E if the graph is undirected The length of a path is its number of edges A path is simple if it doesn’t repeat any vertices A cycle is a path v0, v1, v2, ..., vp such that v0 = vp A cycle is simple if it does not repeat any vertices except the first and last A graph is acyclic if it has no cycles A directed acyclic graph is called a DAG Path A,C,D A B C D E DAG A B C D E Not a DAG

Is this a DAG? Intuition: This idea leads to an algorithm Yes! B D A C Yes! It is a DAG. F E Intuition: If it’s a DAG, there must be a vertex with indegree zero This idea leads to an algorithm A digraph is a DAG if and only if we can iteratively delete indegree-0 vertices until the graph disappears

Topological sort We just computed a topological sort of the DAG 1 2 3 4 5 6 We just computed a topological sort of the DAG This is a numbering of the vertices such that all edges go from lower- to higher-numbered vertices Useful in job scheduling with precedence constraints

Topological sort A B C D E F 1 k= 1 1  2 B D 2 1 A C k= 0; // inv: k nodes have been given numbers in 1..k in such a way that if n1 <= n2, there is no edge from n2 to n1. while (there is a node of in-degree 0) { Let n be a node of in-degree 0; Give it number k; Delete n and all edges leaving it from the graph. k= k+1; } A B C D E F 1 k= 1 1  2 B D 2 1 A C Abstract algorithm Don’t really want to change the graph. Will have to use some data structures to support this efficiently. F E 3 3 2

Graph coloring A coloring of an undirected graph is an assignment of a color to each node such that no two adjacent vertices get the same color How many colors are needed to color this graph? A B C D E F

An application of coloring Vertices are tasks Edge (u, v) is present if tasks u and v each require access to the same shared resource, and thus cannot execute simultaneously Colors are time slots to schedule the tasks Minimum number of colors needed to color the graph = minimum number of time slots required A B C D E F

Coloring a graph How many colors are needed to color the states so that no two adjacent states have the same color? Asked since 1852 1879: Kemp publishes a proof that only 4 colors are needed! 1880: Julius Peterson finds a flaw in Kemp's proof…

Four Color Theorem Every planar graph is 4-colorable [Appel & Haken, 1976] The proof rested on checking that 1,936 special graphs had a certain property. They used a computer to check that those 1,936 graphs had that property! Basically the first time a computer was needed to check something. Caused a lot of controversy. Gries looked at their computer program, a recursive program written in the assembly language of the IBM 7090 computer, and found an error, which was safe (it said something didn’t have the property when it did) and could be fixed. Others did the same. Since then, there have been improvements. And a formal proof has even been done in the Coq proof system.

Planarity A graph is planar if it can be drawn in the plane without any edges crossing Is this graph planar? A B C D E F

Planarity A graph is planar if it can be drawn in the plane without any edges crossing Is this graph planar? Yes! A B C D E F

Planarity A graph is planar if it can be drawn in the plane without any edges crossing Is this graph planar? Yes! A B C D E F

Detecting Planarity Kuratowski's Theorem: A graph is planar if and only if it does not contain a copy of K5 or K3,3 (possibly with other nodes along the edges shown) K5 K3,3

John Hopcroft & Robert Tarjan Turing Award in 1986 “for fundamental achievements in the design and analysis of algorithms and data structures” One of their fundamental achievements was a linear-time algorithm for determining whether a graph is planar. They worked together coincidentally. Hopcroft took a sabbatic at Stanford. There was little room. So he and Tarjan, a PhD student, were asked to share a room, since Tarjan was interested in data structures. And they started talking. The result was several algorithms, including their famous planarity checker.

David Gries & Jinyun Xue Tech Report, 1988 Abstract: We give a rigorous, yet, we hope, readable, presentation of the Hopcroft-Tarjan linear algorithm for testing the planarity of a graph, using more modern principles and techniques for developing and presenting algorithms that have been developed in the past 10-12 years (their algorithm appeared in the early 1970's). Our algorithm not only tests planarity but also constructs a planar embedding, and in a fairly straightforward manner. The paper concludes with a short discussion of the advantages of our approach. Later, Gries looked at their algorithm and had great difficulty understanding it. He and a postdoc developed it afresh using stepwise refinement and loop invariants and other sound principles. They published a tech report on it in 1986.

Bipartite graphs A directed or undirected graph is bipartite if the vertices can be partitioned into two sets such that no edge connects two vertices in the same set The following are equivalent G is bipartite G is 2-colorable G has no cycles of odd length A 1 B 2 C 3 D

Representations of graphs 1 2 3 4 Adjacency List Adjacency Matrix 1 2 3 4 1 2 3 4 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 2 3 4 1

Graph Quiz Which of the following two graphs are DAGs? Directed Acyclic Graph Graph 1: Graph 2: 1 2 3 1 2 3 3 2 1 0 1 1 0 0 0 0 1 0 TODO: make this a piazza poll

Graph 1 3 1 Is this a DAG? 2 3 2 1

Graph 2 3 1 2 Is this a DAG? 1 2 3 1 2 3 0 1 1 0 0 0 0 1 0

Adjacency Matrix vs. Adjacency List 1 2 3 4 1 2 3 4 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 v = number of vertices e = number of edges d(u) = degree of u = no. edges leaving u 2 3 4 1 Matrix Property List Space Time to enumerate all edges Time to answer “Is there an edge from u1 to u2 ?” better for O(v2) O(1) dense graphs O(v + e) O(d(u1)) sparse graphs

Graph algorithms Search Depth-first search Breadth-first search Shortest paths Dijkstra's algorithm Minimum spanning trees Jarnik/Prim/Dijkstra algorithm Kruskal's algorithm