Introduction to Graphs. CS2110, Spring 2011 Cornell University

Similar documents
GRAPHS Lecture 19 CS2110 Spring 2013

GRAPHS Lecture 17 CS2110 Spring 2014

CS 206 Introduction to Computer Science II

CS 310 Advanced Data Structures and Algorithms

Algorithm Design and Analysis

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

CSI 604 Elementary Graph Algorithms

Lecture 9 Graph Traversal

CSE 100: GRAPH ALGORITHMS

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CSE 373: Data Structures and Algorithms

Copyright 2000, Kevin Wayne 1

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Outline. Graphs. Divide and Conquer.

CS 491 CAP Introduction to Graphs and Search

Lecture 22 Tuesday, April 10

Algorithms: Lecture 10. Chalmers University of Technology

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph: representation and traversal

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Fundamental Graph Algorithms Part Four

3.1 Basic Definitions and Applications

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Lecture 3: Graphs and flows

Graph Representations and Traversal

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

Directed acyclic graphs

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

Basic Graph Algorithms (CLRS B.4-B.5, )

1 Connected components in undirected graphs

Reference Sheet for CO142.2 Discrete Mathematics II

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

Minimum Spanning Trees Ch 23 Traversing graphs

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

Homework No. 4 Answers

Change in Schedule. I'm changing the schedule around a bit...

Algorithm Design and Analysis

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Graphs - I CS 2110, Spring 2016

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Inf 2B: Graphs, BFS, DFS

CSE 421 DFS / Topological Ordering

Depth First Search (DFS)

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Copyright 2000, Kevin Wayne 1

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

W4231: Analysis of Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Goals! CSE 417: Algorithms and Computational Complexity!

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

Graph Traversals. CS200 - Graphs 1

CS302 - Data Structures using C++

Announcements Problem Set 4 is out!

More Graph Algorithms

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

Graph Representation

Basic Graph Definitions

Algorithms and Data Structures

3.1 Basic Definitions and Applications

CSE 417: Algorithms and Computational Complexity

CS2 Algorithms and Data Structures Note 9

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Directed Acyclic Graphs & Topological Sort

Graphs. The ultimate data structure. graphs 1

Announcements. Assignment 6 due right now. Assignment 7 (FacePamphlet) out, due next Friday, March 16 at 3:15PM

Graphs and Graph Algorithms. Slides by Larry Ruzzo

Chapter 9 Graph Algorithms

Data Structures and Algorithms. Chapter 7. Graphs

Some major graph problems

Lecture 26: Graphs: Traversal (Part 1)

Chapter 22 Elementary Graph Algorithms

Introduction to Algorithms

CS 206 Introduction to Computer Science II

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Synonyms. Hostile. Chilly. Direct. Sharp

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

TIE Graph algorithms

3.4 Testing Bipartiteness

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Algorithm Design and Analysis

CSC263 Week 8. Larry Zhang.

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Chapter 9 Graph Algorithms

Synonyms. Hostile. Chilly. Direct. Sharp

Transcription:

Introduction to Graphs CS2110, Spring 2011 Cornell University

A graph is a data structure for representing relationships.

Each graph is a set of nodes connected by edges.

Synonym Graph Hostile Slick Icy Chilly Direct Nifty Cool Abrupt Sharp Composed

Goals for Today Learn the formalisms behind graphs. Learn different representations for graphs. Learn about paths and cycles in graphs. See three ways of exploring a graph. Explore applications of graphs to real-world problems. Explore algorithms for drawing graphs.

Formalisms A (directed) graph is a pair G = (V, E) where V are the vertices (nodes) of the graph. E are the edges (arcs) of the graph. Each edge is a pair (u, v) of the start and end (or source and sink) of the edge.

CAN MAN RAN MAT CAT SAT RAT

Directed and Undirected Graphs A graph is directed if its edges specify which is the start and end node. Encodes asymmetric relationship. A graph is undirected if the edges don't distinguish between the start and end nodes. Encodes symmetric relationship. An undirected graph is a special case of a directed graph (just add edges both ways).

How Big is a Graph G = (V, E)? Two measures: Number of vertices: V (often denoted n) Number of edges: E (often denoted m) E can be at most O( V 2 ) A graph is called sparse if it has few edges. A graph with many edges is called dense.

Navigating a Graph B D A F C E

Navigating a Graph B D A F C E

Navigating a Graph B D A F A B D F C E

Navigating a Graph B D A F A C F C E

A path from v 0 to v n is a list of edges (v 0, v 1 ), (v 1, v 2 ),, (v n-1, v n ).

The length of a path is the number of edges it contains.

Navigating a Graph B D A F C E

Navigating a Graph B D A F C E

A node v is reachable from node u if there is a path from u to v.

Navigating a Graph B D A F C E

Navigating a Graph B D A F C E

Navigating a Graph B D A F B D B C E

Navigating a Graph B D A F B D B D B C E

Navigating a Graph B D A F C E

Navigating a Graph B D A F A B D B D F C E

A cycle in a graph is a set of edges (v 0, v 1 ), (v 1, v 2 ),, (v n, v 0 ) that starts and ends at the same node.

A simple path is a path that does not contain a cycle. A simple cycle is a cycle that does not contain a smaller cycle

Properties of Nodes B D A F C E

The indegree of a node is the number of edges entering that node. The outdegree of a node is the number of edges leaving that node. In an undirected graph, these are the same and are called the degree of the node.

Summary of Terminology A path is a series of edges connecting two nodes. The length of a path is the number of edges in the path. A node v is reachable from u if there is a path from u to v. A cycle is a path from a node to itself. A simple path is a path without a cycle. A simple cycle is a cycle that does not contain a nested cycle. The indegree and outdegree of a node are the number of edges entering/leaving it.

Representing Graphs

Adjacency Matrices n x n grid of boolean values. B D Element A ij is 1 if edge from i to j, 0 else. Memory usage is O(n2 ) A C E F Can check if an edge exists in O(1). Can find all edges entering or leaving a node in O(n). A B C D E F A B C D E F 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0

Adjacency Lists List of edges leaving each node. A B D F Memory usage is O(m+n) C E Find edges leaving a node in O(d + (u)) Check if edge exists in O(d + (u)) A B C D B D E B C F F E F C E

Graph Algorithms

Representing Prerequisites Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

A directed acyclic graph (DAG) is a directed graph with no cycles.

Examples of DAGs

Examples of DAGs 4 2 6 1 3 5 7

Examples of DAGs

Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

Traversing a DAG Graph Path Cycle Degree Path Length Simple Path Reachability Simple Cycle

Traversing a DAG Graph Cycle Path Degree Path Length Simple Path Reachability Simple Cycle

Traversing a DAG Graph Cycle Path Degree Path Length Simple Path Reachability Simple Cycle

Traversing a DAG Graph Cycle Simple Cycle Path Degree Path Length Simple Path Reachability

Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Reachability Simple Path

Traversing a DAG Graph Cycle Simple Cycle Degree Path Path Length Simple Path Reachability

Traversing a DAG Graph Cycle Graph Simple Cycle Degree Path Cycle Degree Path Path Length Path Length Simple Path Reachability Simple Cycle Simple Path Reachability

Topological Sort Order the nodes of a DAG so no node is picked before its parents. Algorithm: Find a node with no incoming edges (indegree 0) Remove it from the graph. Add it to the resulting ordering. Not necessarily unique. Question: When is it unique?

Analyzing Topological Sort Assumes at each step that the DAG has a node with indegree zero. Is this always true? Claim one: Every DAG has such a node. Proof sketch: If this isn't true, then each node has at least one incoming edge. Start at any node and keep following backwards across that edge. Eventually you will find the same node twice and have found a cycle. Claim two: Removing such a node leaves the DAG a DAG. Proof sketch: If the resulting graph has a cycle, the old graph had a cycle as well.

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

Traversing an Arbitrary Graph

General Graph Search Algorithm Maintain a collection C of nodes to visit. Initialize C with some set of nodes. While C is not empty: Pick a node v out of C. Follow all outgoing edges from v, adding each unvisited node found this way to C. Eventually explores all nodes reachable from the starting set of nodes. (Why?)

Depth-First Search Specialization of the general search algorithm where nodes to visit are put on a stack. Explores down a path as far as possible, then backs up. Simple graph search algorithm useful for exploring a complete graph. Useful as a subroutine in many important graph algorithms. Runs in O(m + n) with adjacency lists, O(n 2 ) with adjacency matrix.

Depth-first search A B C D E F

Depth-first search A B C D E F Stack

Depth-first search A B C D E F A Stack

Depth-first search A B C D E F Stack

Depth-first search A B C D E F E B Stack

Depth-first search A B C D E F E B Stack

Depth-first search A B C D E F B Stack

Depth-first search A B C D E F C F D B Stack

Depth-first search A B C D E F C F D B Stack

Depth-first search A B C D E F F D B Stack

Depth-first search A B C D E F F D B Stack

Depth-first search A B C D E F F D B Stack

Depth-first search A B C D E F D B Stack

Depth-first search A B C D E F D B Stack

Depth-first search A B C D E F B Stack

Depth-first search A B C D E F B Stack

Depth-first search A B C D E F Stack

Depth-first search A B C D E F Stack

Implementing DFS DFS(Node v, Set<Node> visited) { if (v is in visited) return; Add v to visited; } for (Node u connected to v) DFS(u, visited);

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Graph Search Trees

Mazes as Graphs

Mazes as Graphs

Mazes as Graphs

Mazes as Graphs

Creating a Maze with DFS Create a grid graph of the appropriate size. Starting at any node, run a depth-first search, adding the arcs to the stack in random order. The resulting DFS tree is a maze with one solution.

Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes.

Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. Stack

Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. Stack

Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. A C B Stack

Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. A C B A B C Stack

Problems with DFS Useful when trying to explore everything. Not good at finding specific nodes. A C B A B C Stack

Breadth-First Search Specialization of the general search algorithm where nodes to visit are put into a queue. Explores nodes one hop away, then two hops away, etc. Finds path with fewest edges from start node to all other nodes. Runs in O(m + n) with adjacency lists, O(n 2 ) with adjacency matrix.

Breadth-first search A B C D E F

Breadth-first search A B C D E F Queue

Breadth-first search A B C D E F Queue A

Breadth-first search A B C D E F Queue

Breadth-first search A B C D E F Queue B E

Breadth-first search A B C D E F Queue B E

Breadth-first search A B C D E F Queue E

Breadth-first search A B C D E F Queue E C

Breadth-first search A B C D E F Queue E C

Breadth-first search A B C D E F Queue C

Breadth-first search A B C D E F Queue C D F

Breadth-first search A B C D E F Queue C D F

Breadth-first search A B C D E F Queue D F

Breadth-first search A B C D E F Queue D F

Breadth-first search A B C D E F Queue F

Breadth-first search A B C D E F Queue F

Breadth-first search A B C D E F Queue

Breadth-first search A B C D E F Queue

Implementing BFS BFS(Node v, Set<Node> visited) { Create a Queue<Node> of nodes to visit; Add v to the queue; while (The queue is not empty) { Dequeue a node from the queue, let it be u; if (u has been visited) continue; Add u to the visited set; } } for (Node w connected to u) Enqueue w in the queue;

Classic Graph Algorithms

Graph Coloring Given a graph G, assign colors to the nodes so that no edge has endpoints of the same color. The chromatic number of a graph is the fewest number of colors needed to color it.

Graph Coloring is Hard. Determining whether a graph can be colored with k colors (for k > 2) is NP-complete. It is not known whether this problem can be solved in polynomial time. Want $1,000,000? Find a polynomial-time algorithm or prove that none exists.

Matching A matching in a graph is a subset of the edges that don't share any endpoints. Intuitively, pairing up nodes in the graph.

Matching A matching in a graph is a subset of the edges that don't share any endpoints. Intuitively, pairing up nodes in the graph.

Applications of Matching Unlike graph coloring, matching can be done quickly. Sample application: divvying up desserts.

Divvying Up Desserts

Divvying Up Desserts

Divvying Up Desserts

Divvying Up Desserts

Divvying Up Desserts

Divvying Up Desserts 3 7

Divvying Up Desserts 4 3 7 6

Divvying Up Desserts 4 3 7 6 8 2

Divvying Up Desserts 4 3 7 6 5 8 2 5

Divvying Up Desserts 3 6 8 5

Drawing Graphs

Hostile Slick Icy Chilly Direct Nifty Cool Abrupt Sharp Composed

Chilly Slick Composed Icy Direct Sharp Hostile Nifty Cool Abrupt

Idea: Treat the graph as a physical system that exerts forces on itself.

This is called a force-directed layout algorithm.

Summary Graphs are a powerful abstraction for modeling relationships and connectivity. Adjacency lists and adjacency matrices are two common representations of graphs. Directed acyclic graphs can be visited via a topological sort. Depth-first search is a simple graph exploration algorithm. Breadth-first search searches a graph one layer at a time. There are many classic algorithms on graphs: Graph coloring tries to color nodes so no two nodes of the same color are connected. Matchings represent pairing up of graph elements. Graph drawing seeks to render aesthetically-pleasing graphs.