Graphs Lecture 19 CS2110 – Spring 2013.

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Connected Components, Directed Graphs, Topological Sort COMP171.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Intro to Graphs CSIT 402 Data Structures II. CSIT 402 Graph Introduction2 Graphs Graphs are composed of ›Nodes (vertices) Can be labeled ›Edges (arcs)
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
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 Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
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.
CSC2100B Tutorial 10 Graph Jianye Hao.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Great Theoretical Ideas in Computer Science for Some.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Chapter 05 Introduction to Graph And Search Algorithms.
COMPSCI 102 Introduction to Discrete Mathematics.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs - I CS 2110, Spring 2017 Leonhard Euler. 1736
Graphs Representation, BFS, DFS
School of Computing Clemson University Fall, 2012
Lecture 11 Graph Algorithms
GRAPHS Lecture 16 CS2110 Fall 2017.
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Graph Search Lecture 17 CS 2110 Fall 2017.
Introduction to Graphs
Depth-First Search.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Algorithm.
Graph & BFS.
Graphs Lecture 18 CS2110 – Fall 2009.
Modeling and Simulation NETW 707
Graphs Chapter 13.
Graph Representation (23.1/22.1)
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
GRAPHS Lecture 17 CS2110.
Richard Anderson Autumn 2016 Lecture 5
Graphs Chapter 7 Visit for more Learning Resources.
Text Book: Introduction to algorithms By C L R S
Graphs G = (V, E) V are the vertices; E are the edges.
CSE 417: Algorithms and Computational Complexity
GRAPHS Lecture 17 CS2110 Spring 2018.
Richard Anderson Winter 2019 Lecture 6
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graphs Lecture 19 CS2110 – Spring 2013

Announcements Prelim 2: Two and a half weeks from now Exam conflicts? Tuesday, April16, 7:30-9pm, Statler Exam conflicts? We need to hear about them and can arrange a makeup It would be the same day but 5:30-7:00 Old exams available on the course website

These are not Graphs ...not the kind we mean, anyway

These are Graphs K5 K3,3 =

Applications of Graphs Communication networks Routing and shortest path problems Commodity distribution (flow) Traffic control Resource allocation Geometric modeling ...

Graph Definitions A directed graph (or digraph) is a pair (V, E) where V is a set E is a set of ordered pairs (u,v) where u,v ∈ V Usually require u ≠ v (i.e., no self-loops) An element of V is called a vertex (pl. vertices) or node An element of E is called an edge or arc |V| = size of V, often denoted n |E| = size of E, often denoted m

Example Directed Graph (Digraph) b a c d e f V = { a,b,c,d,e,f } E = { (a,b), (a,c), (a,e), (b,c), (b,d), (b,e), (c,d), (c,f), (d,e), (d,f), (e,f) } |V| = 6, |E| = 11

Example Undirected Graph An undirected graph is just like a directed graph, except the edges are unordered pairs (sets) {u,v} Example: b a c e d f V = { a,b,c,d,e,f } E = { {a,b}, {a,c}, {a,e}, {b,c}, {b,d}, {b,e}, {c,d}, {c,f}, {d,e}, {d,f }, {e,f } }

Some Graph Terminology Vertices u and v are called the source and sink of the directed edge (u,v), respectively Vertices u and v are called the endpoints of (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 b a c d e f b a c e d f

More Graph Terminology v0 v5 A path is a sequence v0,v1,v2,...,vp of vertices such that (vi,vi+1) ∈ E, 0 ≤ i ≤ p – 1 The length of a path is its number of edges In this example, the length is 5 A path is simple if it does not 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 b a c d e f

Is This a Dag? Intuition: This idea leads to an algorithm b a c d e f Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm b d c a f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm b d c f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm b d c f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm c f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm c f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm f e Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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

Is This a Dag? Intuition: This idea leads to an algorithm f Intuition: If it’s a dag, there must be a vertex with indegree zero – why? 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 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 1 2 3 4 5

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?

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? 3

An Application of Coloring Vertices are jobs Edge (u,v) is present if jobs u and v each require access to the same shared resource, and thus cannot execute simultaneously Colors are time slots to schedule the jobs Minimum number of colors needed to color the graph = minimum number of time slots required

Planarity A graph is planar if it can be embedded in the plane with no edges crossing Is this graph planar?

Planarity A graph is planar if it can be embedded in the plane with no edges crossing Is this graph planar? Yes

Planarity A graph is planar if it can be embedded in the plane with no edges crossing Is this graph planar? Yes

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

The Four-Color Theorem Every planar graph is 4-colorable (Appel & Haken, 1976)

Bipartite Graphs A directed or undirected graph is bipartite if the vertices can be partitioned into two sets such that all edges go between the two sets

Bipartite Graphs The following are equivalent G is bipartite G is 2-colorable G has no cycles of odd length

Traveling Salesperson Boston Ithaca Copenhagen 1323 1556 224 132 160 512 New York London 189 216 Amsterdam 210 1078 Paris 1002 Washington Munich 1322 660 1214 505 419 1356 441 1380 1202 Rome Atlanta Find a path of minimum distance that visits every city

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

Adjacency Matrix or Adjacency List? n = number of vertices m = number of edges d(u) = degree of u = number of edges leaving u Adjacency Matrix Uses space O(n2) Can iterate over all edges in time O(n2) Can answer “Is there an edge from u to v?” in O(1) time Better for dense graphs (lots of edges) Adjacency List Uses space O(m+n) Can iterate over all edges in time O(m+n) Can answer “Is there an edge from u to v?” in O(d(u)) time Better for sparse graphs (fewer edges)

Graph Algorithms Search Shortest paths Minimum spanning trees depth-first search breadth-first search Shortest paths Dijkstra's algorithm Minimum spanning trees Prim's algorithm Kruskal's algorithm

Depth-First Search Follow edges depth-first starting from an arbitrary vertex r, using a stack to remember where you came from When you encounter a vertex previously visited, or there are no outgoing edges, retreat and try another path Eventually visit all vertices reachable from r If there are still unvisited vertices, repeat O(m) time

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Depth-First Search

Breadth-First Search Same, except use a queue instead of a stack to determine which edge to explore next Recall: A stack is last-in, first-out (LIFO) A queue is first-in, first-out (FIFO)

Breadth-First Search

Breadth-First Search

Breadth-First Search

Breadth-First Search

Breadth-First Search

Breadth-First Search

Breadth-First Search

Breadth-First Search

Breadth-First Search

Summary We’ve seen an introduction to graphs and will return to this topic next week on Tuesday Definitions Testing for a dag Depth-first and breadth-first search On Thursday Ken and David will be out of town. Dexter Kozen will do a lecture on induction We use induction to prove properties of graphs and graph algorithms, so the fit is good