Introduction to Graphs CS2110, Spring 2011 Cornell University A - - PowerPoint PPT Presentation

introduction to graphs
SMART_READER_LITE
LIVE PREVIEW

Introduction to Graphs CS2110, Spring 2011 Cornell University A - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Introduction to Graphs

CS2110, Spring 2011 Cornell University

slide-2
SLIDE 2

A graph is a data structure for representing relationships.

slide-3
SLIDE 3

Each graph is a set of nodes connected by edges.

slide-4
SLIDE 4

Nifty Cool Sharp Chilly Composed Abrupt Hostile Direct Slick Icy

Synonym Graph

slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10

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.
slide-11
SLIDE 11

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.

slide-12
SLIDE 12
slide-13
SLIDE 13

CAT SAT RAT RAN MAN MAT CAN

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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.

slide-16
SLIDE 16

Navigating a Graph

A B D C E F

slide-17
SLIDE 17

Navigating a Graph

A B D C E F

slide-18
SLIDE 18

Navigating a Graph

A B D C E F

A B D F

slide-19
SLIDE 19

Navigating a Graph

A B D C E F

A C F

slide-20
SLIDE 20

A path from v0 to vn is a list of edges (v0, v1), (v1, v2), …, (vn-1, vn).

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Navigating a Graph

A B D C E F

slide-23
SLIDE 23

Navigating a Graph

A B D C E F

slide-24
SLIDE 24

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

slide-25
SLIDE 25

Navigating a Graph

A B D C E F

slide-26
SLIDE 26

Navigating a Graph

A B D C E F

slide-27
SLIDE 27

Navigating a Graph

A B D C E F

B D B

slide-28
SLIDE 28

Navigating a Graph

A B D C E F

B D B D B

slide-29
SLIDE 29

Navigating a Graph

A B D C E F

slide-30
SLIDE 30

Navigating a Graph

A B D C E F

A B D B D F

slide-31
SLIDE 31

A cycle in a graph is a set of edges (v0, v1), (v1, v2), …, (vn, v0) that starts and ends at the same node.

slide-32
SLIDE 32

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

slide-33
SLIDE 33

Properties of Nodes

A B D C E F

slide-34
SLIDE 34

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.

slide-35
SLIDE 35

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.

slide-36
SLIDE 36

Representing Graphs

slide-37
SLIDE 37

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
  • r leaving a node in O(n).

A B D C E F

1 1 1 A B C D E F A B C D E F 1 1 1 1 1 1

slide-38
SLIDE 38

Adjacency Lists

A B D C E F

A B C D E F B C D E F F B 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))

slide-39
SLIDE 39

Graph Algorithms

slide-40
SLIDE 40

Representing Prerequisites

Graph Path Cycle Simple Path Simple Cycle Path Length Degree Reachability

slide-41
SLIDE 41

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

slide-42
SLIDE 42

Examples of DAGs

slide-43
SLIDE 43

Examples of DAGs

4 2 6 1 3 5 7

slide-44
SLIDE 44

Examples of DAGs

slide-45
SLIDE 45

Traversing a DAG

Graph Path Cycle Simple Path Simple Cycle Path Length Degree Reachability

slide-46
SLIDE 46

Traversing a DAG

Graph Path Cycle Simple Path Simple Cycle Path Length Degree Reachability

slide-47
SLIDE 47

Traversing a DAG

Path Cycle Simple Path Simple Cycle Path Length Degree Reachability Graph

slide-48
SLIDE 48

Traversing a DAG

Path Cycle Simple Path Simple Cycle Path Length Degree Reachability Graph

slide-49
SLIDE 49

Traversing a DAG

Path Simple Path Simple Cycle Path Length Degree Reachability Cycle Graph

slide-50
SLIDE 50

Traversing a DAG

Path Simple Path Simple Cycle Path Length Degree Reachability Cycle Graph

slide-51
SLIDE 51

Traversing a DAG

Path Simple Path Simple Cycle Path Length Degree Reachability Cycle Graph

slide-52
SLIDE 52

Traversing a DAG

Path Simple Path Simple Cycle Path Length Degree Reachability Cycle Graph

slide-53
SLIDE 53

Traversing a DAG

Simple Path Simple Cycle Path Length Degree Reachability Graph Cycle Path

slide-54
SLIDE 54

Traversing a DAG

Simple Path Simple Cycle Path Length Degree Reachability Graph Cycle Path

slide-55
SLIDE 55

Traversing a DAG

Simple Path Simple Cycle Path Length Degree Reachability Graph Cycle Path

slide-56
SLIDE 56

Traversing a DAG

Simple Path Simple Cycle Path Length Degree Reachability Graph Cycle Path

slide-57
SLIDE 57

Traversing a DAG

Simple Path Simple Cycle Path Length Degree Reachability Graph Cycle Path

slide-58
SLIDE 58

Traversing a DAG

Simple Path Simple Cycle Path Length Degree Reachability Graph Cycle Path Graph Path Cycle Simple Path Simple Cycle Path Length Degree Reachability

slide-59
SLIDE 59

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?
slide-60
SLIDE 60

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.

slide-61
SLIDE 61

Traversing an Arbitrary Graph

slide-62
SLIDE 62

Traversing an Arbitrary Graph

slide-63
SLIDE 63

Traversing an Arbitrary Graph

slide-64
SLIDE 64

Traversing an Arbitrary Graph

slide-65
SLIDE 65

Traversing an Arbitrary Graph

slide-66
SLIDE 66

Traversing an Arbitrary Graph

slide-67
SLIDE 67

Traversing an Arbitrary Graph

slide-68
SLIDE 68

Traversing an Arbitrary Graph

slide-69
SLIDE 69

Traversing an Arbitrary Graph

slide-70
SLIDE 70

Traversing an Arbitrary Graph

slide-71
SLIDE 71

Traversing an Arbitrary Graph

slide-72
SLIDE 72

Traversing an Arbitrary Graph

slide-73
SLIDE 73

Traversing an Arbitrary Graph

slide-74
SLIDE 74

Traversing an Arbitrary Graph

slide-75
SLIDE 75

Traversing an Arbitrary Graph

slide-76
SLIDE 76

Traversing an Arbitrary Graph

slide-77
SLIDE 77

Traversing an Arbitrary Graph

slide-78
SLIDE 78

Traversing an Arbitrary Graph

slide-79
SLIDE 79

Traversing an Arbitrary Graph

slide-80
SLIDE 80

Traversing an Arbitrary Graph

slide-81
SLIDE 81

Traversing an Arbitrary Graph

slide-82
SLIDE 82

Traversing an Arbitrary Graph

slide-83
SLIDE 83

Traversing an Arbitrary Graph

slide-84
SLIDE 84

Traversing an Arbitrary Graph

slide-85
SLIDE 85

Traversing an Arbitrary Graph

slide-86
SLIDE 86

Traversing an Arbitrary Graph

slide-87
SLIDE 87

Traversing an Arbitrary Graph

slide-88
SLIDE 88

Traversing an Arbitrary Graph

slide-89
SLIDE 89

Traversing an Arbitrary Graph

slide-90
SLIDE 90

Traversing an Arbitrary Graph

slide-91
SLIDE 91

Traversing an Arbitrary Graph

slide-92
SLIDE 92

Traversing an Arbitrary Graph

slide-93
SLIDE 93

Traversing an Arbitrary Graph

slide-94
SLIDE 94

Traversing an Arbitrary Graph

slide-95
SLIDE 95

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

slide-96
SLIDE 96

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.

slide-97
SLIDE 97

Depth-first search

A B D E C F

slide-98
SLIDE 98

Depth-first search

A B D E C F Stack

slide-99
SLIDE 99

Depth-first search

A B D E C F Stack A

slide-100
SLIDE 100

Depth-first search

A B D E C F Stack

slide-101
SLIDE 101

Depth-first search

A B D E C F Stack B E

slide-102
SLIDE 102

Depth-first search

A B D E C F Stack B E

slide-103
SLIDE 103

Depth-first search

A B D E C F Stack B

slide-104
SLIDE 104

Depth-first search

A B D E C F Stack B D F C

slide-105
SLIDE 105

Depth-first search

A B D E C F Stack B D F C

slide-106
SLIDE 106

Depth-first search

A B D E C F Stack B D F

slide-107
SLIDE 107

Depth-first search

A B D E C F Stack B D F

slide-108
SLIDE 108

Depth-first search

A B D E C F Stack B D F

slide-109
SLIDE 109

Depth-first search

A B D E C F Stack B D

slide-110
SLIDE 110

Depth-first search

A B D E C F Stack B D

slide-111
SLIDE 111

Depth-first search

A B D E C F Stack B

slide-112
SLIDE 112

Depth-first search

A B D E C F Stack B

slide-113
SLIDE 113

Depth-first search

A B D E C F Stack

slide-114
SLIDE 114

Depth-first search

A B D E C F Stack

slide-115
SLIDE 115

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

slide-116
SLIDE 116

Graph Search Trees

slide-117
SLIDE 117

Graph Search Trees

slide-118
SLIDE 118

Graph Search Trees

slide-119
SLIDE 119

Graph Search Trees

slide-120
SLIDE 120

Graph Search Trees

slide-121
SLIDE 121

Graph Search Trees

slide-122
SLIDE 122

Graph Search Trees

slide-123
SLIDE 123

Graph Search Trees

slide-124
SLIDE 124

Graph Search Trees

slide-125
SLIDE 125

Graph Search Trees

slide-126
SLIDE 126

Graph Search Trees

slide-127
SLIDE 127

Graph Search Trees

slide-128
SLIDE 128

Graph Search Trees

slide-129
SLIDE 129

Graph Search Trees

slide-130
SLIDE 130

Graph Search Trees

slide-131
SLIDE 131

Graph Search Trees

slide-132
SLIDE 132

Graph Search Trees

slide-133
SLIDE 133

Graph Search Trees

slide-134
SLIDE 134

Graph Search Trees

slide-135
SLIDE 135

Graph Search Trees

slide-136
SLIDE 136

Graph Search Trees

slide-137
SLIDE 137

Mazes as Graphs

slide-138
SLIDE 138

Mazes as Graphs

slide-139
SLIDE 139

Mazes as Graphs

slide-140
SLIDE 140

Mazes as Graphs

slide-141
SLIDE 141

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.

slide-142
SLIDE 142

Problems with DFS

  • Useful when trying to explore everything.
  • Not good at finding specific nodes.
slide-143
SLIDE 143

Problems with DFS

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

Stack

slide-144
SLIDE 144

Problems with DFS

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

Stack

slide-145
SLIDE 145

Problems with DFS

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

A B C Stack

slide-146
SLIDE 146

Problems with DFS

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

A B C Stack C B A

slide-147
SLIDE 147

Problems with DFS

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

A B C Stack C B A

slide-148
SLIDE 148

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.

slide-149
SLIDE 149

Breadth-first search

A B D E C F

slide-150
SLIDE 150

Breadth-first search

A B D E C F Queue

slide-151
SLIDE 151

Breadth-first search

A B D E C F Queue A

slide-152
SLIDE 152

Breadth-first search

A B D E C F Queue

slide-153
SLIDE 153

Breadth-first search

A B D E C F Queue B E

slide-154
SLIDE 154

Breadth-first search

A B D E C F Queue B E

slide-155
SLIDE 155

Breadth-first search

A B D E C F Queue E

slide-156
SLIDE 156

Breadth-first search

A B D E C F Queue E C

slide-157
SLIDE 157

Breadth-first search

A B D E C F Queue E C

slide-158
SLIDE 158

Breadth-first search

A B D E C F Queue C

slide-159
SLIDE 159

Breadth-first search

A B D E C F Queue C D F

slide-160
SLIDE 160

Breadth-first search

A B D E C F Queue C D F

slide-161
SLIDE 161

Breadth-first search

A B D E C F Queue D F

slide-162
SLIDE 162

Breadth-first search

A B D E C F Queue D F

slide-163
SLIDE 163

Breadth-first search

A B D E C F Queue F

slide-164
SLIDE 164

Breadth-first search

A B D E C F Queue F

slide-165
SLIDE 165

Breadth-first search

A B D E C F Queue

slide-166
SLIDE 166

Breadth-first search

A B D E C F Queue

slide-167
SLIDE 167

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

slide-168
SLIDE 168

Classic Graph Algorithms

slide-169
SLIDE 169

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.

slide-170
SLIDE 170

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.

slide-171
SLIDE 171

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.
slide-172
SLIDE 172

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.
slide-173
SLIDE 173

Applications of Matching

  • Unlike graph coloring, matching can be done

quickly.

  • Sample application: divvying up desserts.
slide-174
SLIDE 174

Divvying Up Desserts

slide-175
SLIDE 175

Divvying Up Desserts

slide-176
SLIDE 176

Divvying Up Desserts

slide-177
SLIDE 177

Divvying Up Desserts

slide-178
SLIDE 178

Divvying Up Desserts

slide-179
SLIDE 179

Divvying Up Desserts

3 7

slide-180
SLIDE 180

Divvying Up Desserts

3 7 4 6

slide-181
SLIDE 181

Divvying Up Desserts

3 7 4 6 2 8

slide-182
SLIDE 182

Divvying Up Desserts

3 7 4 6 2 8 5 5

slide-183
SLIDE 183

Divvying Up Desserts

3 6 8 5

slide-184
SLIDE 184

Drawing Graphs

slide-185
SLIDE 185

Nifty Cool Sharp Chilly Composed Abrupt Hostile Direct Slick Icy

slide-186
SLIDE 186

Nifty Cool Sharp Chilly Composed Abrupt Hostile Direct Slick Icy

slide-187
SLIDE 187
slide-188
SLIDE 188
slide-189
SLIDE 189
slide-190
SLIDE 190
slide-191
SLIDE 191

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

slide-192
SLIDE 192

This is called a force-directed layout algorithm.

slide-193
SLIDE 193

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.