+ All Categories
Home > Documents > Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell...

Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell...

Date post: 05-Mar-2021
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
193
Introduction to Graphs CS2110, Spring 2011 Cornell University
Transcript
Page 1: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Introduction to Graphs

CS2110, Spring 2011Cornell University

Page 2: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

A graph is a data structure for representing relationships.

Page 3: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Each graph is a set of nodes connected by edges.

Page 4: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Nifty Cool

Sharp

Chilly

ComposedAbrupt

Hostile

Direct

Slick Icy

Synonym Graph

Page 5: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 6: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 7: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 8: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 9: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 10: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 11: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 12: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 13: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

CAT SAT RAT

RANMAN

MAT

CAN

Page 14: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 15: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 16: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 17: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 18: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

A B D F

Page 19: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

A C F

Page 20: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

A path from v0 to v

n is a list of edges

(v0, v

1), (v

1, v

2), …, (v

n-1, v

n).

Page 21: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 22: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 23: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 24: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 25: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 26: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 27: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

B D B

Page 28: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

B D B D B

Page 29: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

Page 30: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Navigating a Graph

A

B D

C E

F

A B D B D F

Page 31: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

A cycle in a graph is a set of edges

(v0, v

1), (v

1, v

2), …, (v

n, v

0)

that starts and ends at the same node.

Page 32: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 33: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Properties of Nodes

A

B D

C E

F

Page 34: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

The indegree of a node is the number ofedges entering that node.

The outdegree of a node is the number ofedges leaving that node.

In an undirected graph, these are the same and are called the degree of the node.

Page 35: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 36: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Representing Graphs

Page 37: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Adjacency Matrices

● n x n grid of boolean values.

● Element Aij is 1 if edge from i

to j, 0 else.

● Memory usage is O(n2)

● Can check if an edge exists in O(1).

● Can find all edges entering or leaving a node in O(n).

A

B D

C E

F

0 1 1 0 0 0

0 0 0 1 0 0

A B C D E F

A

B

C

D

E

F

0 0 0 0 1 1

0 1 0 0 0 1

0 0 0 0 0 0

0 0 1 0 1 0

Page 38: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Adjacency Lists

A

B D

C E

F

A

B

C

D

E

F

B C

D

E F

FB

C E

● List of edges leaving each node.

● Memory usage is O(m+n)

● Find edges leaving a node in O(d+ (u))

● Check if edge exists in O(d+ (u))

Page 39: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Algorithms

Page 40: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Representing Prerequisites

Graph

Path Cycle

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Page 41: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 42: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Examples of DAGs

Page 43: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Examples of DAGs

4

2 6

1 3 5 7

Page 44: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Examples of DAGs

Page 45: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Graph

Path Cycle

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Page 46: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Graph

Path Cycle

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Page 47: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Path Cycle

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Page 48: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Path Cycle

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Page 49: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Path

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Cycle

Graph

Page 50: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Path

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Cycle

Graph

Page 51: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Path

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Cycle

Graph

Page 52: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

Path

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Cycle

Graph

Page 53: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Cycle

Path

Page 54: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Cycle

Path

Page 55: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Cycle

Path

Page 56: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Cycle

Path

Page 57: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Cycle

Path

Page 58: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing a DAG

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Graph

Cycle

Path

Graph

Path Cycle

SimplePath

SimpleCycle

PathLength

Degree

Reachability

Page 59: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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?

Page 60: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 61: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 62: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 63: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 64: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 65: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 66: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 67: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 68: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 69: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 70: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 71: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 72: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 73: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 74: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 75: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 76: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 77: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 78: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 79: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 80: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 81: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 82: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 83: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 84: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 85: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 86: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 87: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 88: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 89: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 90: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 91: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 92: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 93: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 94: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Traversing an Arbitrary Graph

Page 95: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 96: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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(n2 ) with adjacency matrix.

Page 97: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Page 98: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

Page 99: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

A

Page 100: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

Page 101: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

E

Page 102: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

E

Page 103: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

Page 104: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

F

C

Page 105: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

F

C

Page 106: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

F

Page 107: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

F

Page 108: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

F

Page 109: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

Page 110: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

D

Page 111: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

Page 112: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

B

Page 113: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

Page 114: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Depth-first search

A B

D E

C

F

Stack

Page 115: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 116: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 117: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 118: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 119: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 120: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 121: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 122: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 123: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 124: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 125: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 126: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 127: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 128: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 129: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 130: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 131: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 132: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 133: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 134: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 135: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 136: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Graph Search Trees

Page 137: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Mazes as Graphs

Page 138: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Mazes as Graphs

Page 139: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Mazes as Graphs

Page 140: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Mazes as Graphs

Page 141: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 142: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Problems with DFS

● Useful when trying to explore everything.● Not good at finding specific nodes.

Page 143: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Problems with DFS

● Useful when trying to explore everything.● Not good at finding specific nodes.

Stack

Page 144: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Problems with DFS

● Useful when trying to explore everything.● Not good at finding specific nodes.

Stack

Page 145: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Problems with DFS

● Useful when trying to explore everything.● Not good at finding specific nodes.

A

B

C

Stack

Page 146: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Problems with DFS

● Useful when trying to explore everything.● Not good at finding specific nodes.

A

B

C

Stack

C

B

A

Page 147: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Problems with DFS

● Useful when trying to explore everything.● Not good at finding specific nodes.

A

B

C

Stack

C

B

A

Page 148: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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(n2 ) with adjacency matrix.

Page 149: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Page 150: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue

Page 151: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue A

Page 152: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue

Page 153: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue B E

Page 154: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue B E

Page 155: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue E

Page 156: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue E C

Page 157: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue E C

Page 158: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue C

Page 159: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue C D F

Page 160: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue C D F

Page 161: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue D F

Page 162: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue D F

Page 163: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue F

Page 164: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue F

Page 165: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue

Page 166: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Breadth-first search

A B

D E

C

F

Queue

Page 167: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 168: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Classic Graph Algorithms

Page 169: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 170: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 171: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 172: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.

Page 173: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Applications of Matching

● Unlike graph coloring, matching can be done quickly.

● Sample application: divvying up desserts.

Page 174: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

Page 175: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

Page 176: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

Page 177: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

Page 178: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

Page 179: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

3 7

Page 180: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

3 7 4 6

Page 181: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

3 7 4

6 2 8

Page 182: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

3 7 4

6 2 85 5

Page 183: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Divvying Up Desserts

3 6 8 5

Page 184: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Drawing Graphs

Page 185: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Nifty Cool

Sharp

Chilly

ComposedAbrupt

Hostile

Direct

Slick Icy

Page 186: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

Nifty Cool

Sharp

Chilly

Composed

Abrupt

Hostile

Direct

Slick

Icy

Page 187: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 188: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 189: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 190: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a
Page 191: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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

Page 192: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

This is called a force-directed layout algorithm.

Page 193: Introduction to Graphs - Cornell UniversityIntroduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a

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.


Recommended