Lecture 12: Introduction to Graphs and Trees

150
Lecture 12: Introduction to Graphs and Trees CS 5002: Discrete Math Tamara Bonaci, Adrienne Slaughter Northeastern University November 29, 2018 CS 5002: Discrete Math ©Northeastern University Fall 2018 1

Transcript of Lecture 12: Introduction to Graphs and Trees

Page 1: Lecture 12: Introduction to Graphs and Trees

Lecture 12: Introduction to Graphs and Trees

CS 5002: Discrete Math

Tamara Bonaci, Adrienne Slaughter

Northeastern University

November 29, 2018

CS 5002: Discrete Math ©Northeastern University Fall 2018 1

Page 2: Lecture 12: Introduction to Graphs and Trees

1 Review: Proof Techniques

2 Some Graph and Tree Problems

3 Introduction to Trees

4 Special Trees

5 Tree Traversals

6 Introduction to Graphs

7 Graph Representations

8 Graph Traversals

9 Path Finding in a Graph

CS 5002: Discrete Math ©Northeastern University Fall 2018 2

Page 3: Lecture 12: Introduction to Graphs and Trees

Section 1

Review: Proof Techniques

CS 5002: Discrete Math ©Northeastern University Fall 2018 3

Page 4: Lecture 12: Introduction to Graphs and Trees

Proving Correctness

How to prove that an algorithm is correct?

Proof by:

� Counterexample (indirect proof )

� Induction (direct proof )

� Loop Invariant

Other approaches: proof by cases/enumeration, proof by chain of i�s, proof

by contradiction, proof by contrapositive

CS 5002: Discrete Math ©Northeastern University Fall 2018 4

Page 5: Lecture 12: Introduction to Graphs and Trees

Proof by Counterexample

Searching for counterexamples is the best way to disprove the correctness

of some things.

� Identify a case for which something is NOT true

� If the proof seems hard or tricky, sometimes a counterexample works

� Sometimes a counterexample is just easy to see, and can shortcut a

proof

� If a counterexample is hard to find, a proof might be easier

CS 5002: Discrete Math ©Northeastern University Fall 2018 5

Page 6: Lecture 12: Introduction to Graphs and Trees

Proof by Induction

Failure to find a counterexample to a given algorithm does not mean “it is

obvious” that the algorithm is correct.

Mathematical induction is a very useful method for proving the correctness

of recursive algorithms.

1 Prove base case

2 Assume true for arbitrary value n

3 Prove true for case n+ 1

CS 5002: Discrete Math ©Northeastern University Fall 2018 6

Page 7: Lecture 12: Introduction to Graphs and Trees

Proof by Loop Invariant

� Built o� proof by induction.

� Useful for algorithms that loop.

Formally: find loop invariant, then prove:

1 Define a Loop Invariant

2 Initialization

3 Maintenance

4 Termination

Informally:

1 Find p, a loop invariant

2 Show the base case for p

3 Use induction to show the rest.

CS 5002: Discrete Math ©Northeastern University Fall 2018 7

Page 8: Lecture 12: Introduction to Graphs and Trees

Proof by Loop Invariant Is…

Invariant: something that is always true

A�er finding a candidate loop invariant, we prove:

1 Initialization: How does the invariant get initialized?

2 Loop Maintenance: How does the invariant change at each pass

through the loop?

3 Termination: Does the loop stop? When?

CS 5002: Discrete Math ©Northeastern University Fall 2018 8

Page 9: Lecture 12: Introduction to Graphs and Trees

Steps to Loop Invariant Proof

A�er finding your loop invariant:

� Initialization

� Prior to the loop initiating, does the property hold?

� Maintenance

� A�er each loop iteration, does the property still hold, given the

initialization properties?

� Termination

� A�er the loop terminates, does the property still hold? And for what

data?

CS 5002: Discrete Math ©Northeastern University Fall 2018 9

Page 10: Lecture 12: Introduction to Graphs and Trees

Things to remember

� Algorithm termination is necessary for proving correctness of the

entire algorithm.

� Loop invariant termination is necessary for proving the behavior of

the given loop.

CS 5002: Discrete Math ©Northeastern University Fall 2018 10

Page 11: Lecture 12: Introduction to Graphs and Trees

Summary

� Approaches to proving algorithms correct

� Counterexamples

� Helpful for greedy algorithms, heuristics

� Induction

� Based on mathematical induction

� Once we prove a theorem, can use it to build an algorithm

� Loop Invariant

� Based on induction

� Key is finding an invariant

� Lots of examples

CS 5002: Discrete Math ©Northeastern University Fall 2018 11

Page 12: Lecture 12: Introduction to Graphs and Trees

Section 2

Some Graph and Tree Problems

CS 5002: Discrete Math ©Northeastern University Fall 2018 12

Page 13: Lecture 12: Introduction to Graphs and Trees

Outdoors NavigationCS 5002: Discrete and Data Structures Spring 2018

© Northeastern University 128CS 5002: Discrete Math ©Northeastern University Fall 2018 13

Page 14: Lecture 12: Introduction to Graphs and Trees

Indoors Navigation

CS 5002: Discrete Math ©Northeastern University Fall 2018 14

Page 15: Lecture 12: Introduction to Graphs and Trees

Telecommunication Networks

CS 5002: Discrete Math ©Northeastern University Fall 2018 15

Page 16: Lecture 12: Introduction to Graphs and Trees

Social Networks

CS 5002: Discrete Math ©Northeastern University Fall 2018 16

Page 17: Lecture 12: Introduction to Graphs and Trees

Section 3

Introduction to Trees

CS 5002: Discrete Math ©Northeastern University Fall 2018 17

Page 18: Lecture 12: Introduction to Graphs and Trees

What is a Tree?

Tree - a directed, acyclic structure of linked nodes

� Directed - one-way links between nodes (no cycles)

� Acyclic - no path wraps back around to the same node twice (typically

represents hierarchical data)

CS 5002: Discrete Math ©Northeastern University Fall 2018 18

Page 19: Lecture 12: Introduction to Graphs and Trees

Tree Terminology: Nodes

� Tree - a directed, acyclic structure of linked nodes

� Node - an object containing a data value and links to other nodes

� All the blue circles

CS 5002: Discrete Math ©Northeastern University Fall 2018 19

Page 20: Lecture 12: Introduction to Graphs and Trees

Tree Terminology: Edges

� Tree - a directed, acyclic structure of linked nodes

� Edge - directed link, representing relationships between nodes

� All the grey lines

CS 5002: Discrete Math ©Northeastern University Fall 2018 20

Page 21: Lecture 12: Introduction to Graphs and Trees

Root Node

� Tree - a directed, acyclic structure of linked nodes

� Root - the start of the tree tree)� The top-most node in the tree

� Node without parents

CS 5002: Discrete Math ©Northeastern University Fall 2018 21

Page 22: Lecture 12: Introduction to Graphs and Trees

Parent Nodes

� Tree - a directed, acyclic structure of linked nodes

� Parent (ancestor) - any node with at least one child

� The blue nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 22

Page 23: Lecture 12: Introduction to Graphs and Trees

Children Nodes

� Tree - a directed, acyclic structure of linked nodes

� Child (descendant) - any node with a parent

� The blue nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 23

Page 24: Lecture 12: Introduction to Graphs and Trees

Sibling Nodes

� Tree - a directed, acyclic structure of linked nodes

� Siblings - all nodes on the same level

� The blue nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 24

Page 25: Lecture 12: Introduction to Graphs and Trees

Internal Nodes

� Tree - a directed, acyclic structure of linked nodes

� Internal node - a node with at least one children (except root)

� All the orange nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 25

Page 26: Lecture 12: Introduction to Graphs and Trees

Leaf (External) Nodes

� Tree - a directed, acyclic structure of linked nodes

� External node - a node without children

� All the orange nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 26

Page 27: Lecture 12: Introduction to Graphs and Trees

Tree Path

� Tree - a directed, acyclic structure of linked nodes

� Path - a sequence of edges that connects two nodes

� All the orange nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 27

Page 28: Lecture 12: Introduction to Graphs and Trees

Node Level

� Node level - 1 + [the number of connections between the node and

the root]

� The level of node 1 is 1

� The level of node 11 is 4

CS 5002: Discrete Math ©Northeastern University Fall 2018 28

Page 29: Lecture 12: Introduction to Graphs and Trees

Node Height

� Node height - the length of the longest path from the node to some

leaf

� The height of any leaf node is 0� The height of node 8 is 1

� The height of node 1 is 3

� The height of node 11 is 0

CS 5002: Discrete Math ©Northeastern University Fall 2018 29

Page 30: Lecture 12: Introduction to Graphs and Trees

Tree Height

Tree height� The height of the root of the tree, or

� The number of levels of a tree -1.

� The height of the given tree is 3.

CS 5002: Discrete Math ©Northeastern University Fall 2018 30

Page 31: Lecture 12: Introduction to Graphs and Trees

What is Not a Tree?

Problems:

� Cycles: the only node has a cycle

� No root: the only node has a parent (itself, because of the cycle), so

there is no root

CS 5002: Discrete Math ©Northeastern University Fall 2018 31

Page 32: Lecture 12: Introduction to Graphs and Trees

What is Not a Tree?

Problems:

� Cycles: there is a cycle in the tree

� Multiple parents: node 3 has multiple parents on di�erent levels

CS 5002: Discrete Math ©Northeastern University Fall 2018 32

Page 33: Lecture 12: Introduction to Graphs and Trees

What is Not a Tree?

Problems:

� Cycles: there is an undirected cycle in the tree

� Multiple parents: node 5 has multiple parents on di�erent levels

CS 5002: Discrete Math ©Northeastern University Fall 2018 33

Page 34: Lecture 12: Introduction to Graphs and Trees

What is Not a Tree?

Problems:

� Disconnected components: there are two unconnected groups of

nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 34

Page 35: Lecture 12: Introduction to Graphs and Trees

Summary: What is a Tree?

� A tree is a set of nodes, and that set can be empty

� If the tree is not empty, there exists a special node called a root� The root can have multiple children, each of which can be the root of a

subtree

CS 5002: Discrete Math ©Northeastern University Fall 2018 35

Page 36: Lecture 12: Introduction to Graphs and Trees

Section 4

Special Trees

CS 5002: Discrete Math ©Northeastern University Fall 2018 36

Page 37: Lecture 12: Introduction to Graphs and Trees

Special Trees

� Binary Tree

� Binary Search Tree

� Balanced Tree

� Binary Heap/Priority �eue

� Red-Black Tree

CS 5002: Discrete Math ©Northeastern University Fall 2018 37

Page 38: Lecture 12: Introduction to Graphs and Trees

Binary Trees

Binary tree - a tree where every node has at most two children

CS 5002: Discrete Math ©Northeastern University Fall 2018 38

Page 39: Lecture 12: Introduction to Graphs and Trees

Binary Search Trees

� Binary search tree (BST) - a tree where nodes are organized in a

sorted order to make it easier to search

� At every node, you are guaranteed:

� All nodes rooted at the le� child are smaller than the current node

value

� All nodes rooted at the right child are smaller than the current node

value

CS 5002: Discrete Math ©Northeastern University Fall 2018 39

Page 40: Lecture 12: Introduction to Graphs and Trees

Example: Binary Search Trees?

Binary search tree (BST) - a tree where nodes are organized in a sortedorder to make it easier to search

� Le� tree is a BST

� Right tree is not a BST - node 7 is on the le� hand-side of the root

node, and yet it is greater than it

CS 5002: Discrete Math ©Northeastern University Fall 2018 40

Page 41: Lecture 12: Introduction to Graphs and Trees

Example: Using BSTs

Suppose we want to find who has the score of 15…

CS 5002: Discrete Math ©Northeastern University Fall 2018 41

Page 42: Lecture 12: Introduction to Graphs and Trees

Example: Using BSTs

Suppose we want to find who has the score of 15:

� Start at the root

� If the score is > 15, go to the le�

� If the score is < 15, go to the right

� If the score == 15, stop

CS 5002: Discrete Math ©Northeastern University Fall 2018 42

Page 43: Lecture 12: Introduction to Graphs and Trees

Example: Using BSTs

Suppose we want to find who has the score of 15:

� Start at the root

� If the score is > 15, go to the le�

� If the score is < 15, go to the right

� If the score == 15, stop

CS 5002: Discrete Math ©Northeastern University Fall 2018 43

Page 44: Lecture 12: Introduction to Graphs and Trees

Example: Using BSTs

Suppose we want to find who has the score of 15:

� Start at the root

� If the score is > 15, go to the le�

� If the score is < 15, go to the right

� If the score == 15, stop

CS 5002: Discrete Math ©Northeastern University Fall 2018 44

Page 45: Lecture 12: Introduction to Graphs and Trees

Example: Using BSTs

Suppose we want to find who has the score of 15:

� Start at the root

� If the score is > 15, go to the le�

� If the score is < 15, go to the right

� If the score == 15, stop

CS 5002: Discrete Math ©Northeastern University Fall 2018 45

Page 46: Lecture 12: Introduction to Graphs and Trees

Balanced Trees

Consider the following two trees. Which tree would it make it easier for us

to search for an element?

CS 5002: Discrete Math ©Northeastern University Fall 2018 46

Page 47: Lecture 12: Introduction to Graphs and Trees

Balanced Trees

Consider the following two trees. Which tree would it make it easier for us

to search for an element?

Observation: height is o�en key for how fast functions on our trees are.

So, if we can, we want to choose a balanced tree.

CS 5002: Discrete Math ©Northeastern University Fall 2018 47

Page 48: Lecture 12: Introduction to Graphs and Trees

Tree Balance and Height

How do we define balance?� If the heights of the le� and right trees are balanced, the tree is

balanced, so:

|(height(le�)− height(right))|� Anything wrong with this approach?

� Are these trees balanced?

CS 5002: Discrete Math ©Northeastern University Fall 2018 48

Page 49: Lecture 12: Introduction to Graphs and Trees

Tree Balance and Height

How do we define balance?� If the heights of the le� and right trees are balanced, the tree is

balanced, so:

|(height(le�)− height(right))|

� Anything wrong with this approach?

� Are these trees balanced?

CS 5002: Discrete Math ©Northeastern University Fall 2018 49

Page 50: Lecture 12: Introduction to Graphs and Trees

Tree Balance and Height

How do we define balance?� If the heights of the le� and right trees are balanced, the tree is

balanced, so:

|(height(le�)− height(right))|� Anything wrong with this approach?

� Are these trees balanced?

CS 5002: Discrete Math ©Northeastern University Fall 2018 50

Page 51: Lecture 12: Introduction to Graphs and Trees

Tree Balance and Height

How do we define balance?� If the heights of the le� and right trees are balanced, the tree is

balanced, so:

|(height(le�)− height(right))|� Anything wrong with this approach?

� Are these trees balanced?

CS 5002: Discrete Math ©Northeastern University Fall 2018 51

Page 52: Lecture 12: Introduction to Graphs and Trees

Tree Balance and Height

� Observation: it is not enough to balance only root, all nodes should

be balanced.

� The balancing condition: the heights of all le� and right subtrees

di�er by at most 1

CS 5002: Discrete Math ©Northeastern University Fall 2018 52

Page 53: Lecture 12: Introduction to Graphs and Trees

Example: Balanced Trees?

� The le� tree is balanced.

� The right tree is not balanced. The height di�erence between nodes 2

and 8 is two.

CS 5002: Discrete Math ©Northeastern University Fall 2018 53

Page 54: Lecture 12: Introduction to Graphs and Trees

Section 5

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 54

Page 55: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

Challenge: write a recursive function that starts at the root, and prints out

the data in each node of the tree below

CS 5002: Discrete Math ©Northeastern University Fall 2018 55

Page 56: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 56

Page 57: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 57

Page 58: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 58

Page 59: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 59

Page 60: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 60

Page 61: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 61

Page 62: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 62

Page 63: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 63

Page 64: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 64

Page 65: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 65

Page 66: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

Summary:

CS 5002: Discrete Math ©Northeastern University Fall 2018 66

Page 67: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 67

Page 68: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 68

Page 69: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

Challenge: write a non-recursive function that starts at the root, and prints

out the data in each node of the tree below

CS 5002: Discrete Math ©Northeastern University Fall 2018 69

Page 70: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 70

Page 71: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 71

Page 72: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 72

Page 73: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 73

Page 74: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 74

Page 75: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 75

Page 76: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 76

Page 77: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 77

Page 78: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 78

Page 79: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 79

Page 80: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 80

Page 81: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 81

Page 82: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 82

Page 83: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 83

Page 84: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 84

Page 85: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 85

Page 86: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 86

Page 87: Lecture 12: Introduction to Graphs and Trees

Tree Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 87

Page 88: Lecture 12: Introduction to Graphs and Trees

BFS Example

Find element with value 15 in the tree below.

� BFS: traverse all of the nodes on the same level first, and then move on

to the next (lower) level

CS 5002: Discrete Math ©Northeastern University Fall 2018 88

Page 89: Lecture 12: Introduction to Graphs and Trees

BFS Example

Find element with value 15 in the tree below using BFS.

� BFS: traverse all of the nodes on the same level first, and then move on

to the next (lower) level

25 – 10 – 12 – 7 – 8 – 15 – 5

CS 5002: Discrete Math ©Northeastern University Fall 2018 89

Page 90: Lecture 12: Introduction to Graphs and Trees

DFS Example

Find element with value 15 in the tree below using DFS.

� DFS: traverse one side of the tree all the way to the leaves, followed by

the other side

CS 5002: Discrete Math ©Northeastern University Fall 2018 90

Page 91: Lecture 12: Introduction to Graphs and Trees

DFS Example

Find element with value 15 in the tree below using DFS.

� DFS: traverse one side of the tree all the way to the leaves, followed by

the other side

25 – 10 – 7 –8 – 12 – 15 – 5

CS 5002: Discrete Math ©Northeastern University Fall 2018 91

Page 92: Lecture 12: Introduction to Graphs and Trees

Tree Traversals Example

Traverse the tree below, using:

� Pre-order traversal: 25 – 10 – 7 – 8 – 12 – 15 – 5

� In-order traversal: 7 – 10 – 8 – 25 – 15 – 12 – 5

� Post-order traversal: 7 – 8 –10 – 15 –5 – 12 – 25

CS 5002: Discrete Math ©Northeastern University Fall 2018 92

Page 93: Lecture 12: Introduction to Graphs and Trees

Tree Traversals Example

Traverse the tree below, using:

� Pre-order traversal: 25 – 10 – 7 – 8 – 12 – 15 – 5

� In-order traversal: 7 – 10 – 8 – 25 – 15 – 12 – 5

� Post-order traversal: 7 – 8 –10 – 15 –5 – 12 – 25

CS 5002: Discrete Math ©Northeastern University Fall 2018 93

Page 94: Lecture 12: Introduction to Graphs and Trees

Tree Traversals Example

Traverse the tree below, using:

� Pre-order traversal: 25 – 10 – 7 – 8 – 12 – 15 – 5

� In-order traversal: 7 – 10 – 8 – 25 – 15 – 12 – 5

� Post-order traversal: 7 – 8 –10 – 15 –5 – 12 – 25

CS 5002: Discrete Math ©Northeastern University Fall 2018 94

Page 95: Lecture 12: Introduction to Graphs and Trees

Section 6

Introduction to Graphs

CS 5002: Discrete Math ©Northeastern University Fall 2018 95

Page 96: Lecture 12: Introduction to Graphs and Trees

What is a Graph?

Formal Definition:� A graph G is a pair (V,E) where

� V is a set of vertices or nodes

� E is a set of edges that connect vertices

Simply put:� A graph is a collection of nodes (vertices) and edges

� Linked lists, trees, and heaps are all special cases of graphs

CS 5002: Discrete Math ©Northeastern University Fall 2018 96

Page 97: Lecture 12: Introduction to Graphs and Trees

An Example

Here is a graph G = (V,E)

� Each edge is a pair (v1, v2),where v1, v2 are vertices in V

� V = {A,B,C,D,E, F}� E = {(A,B), (A,D), (B,C),

(C,D), (C,E), (D,E)}A

B

C

D

E

F

CS 5002: Discrete Math ©Northeastern University Fall 2018 97

Page 98: Lecture 12: Introduction to Graphs and Trees

Terminology: Undirected Graph

� Two vertices u and v are adjacent in an undirected graph G if {u, v} isan edge in G

� edge e = {u, v} is incident with vertex u and vertex v

� The degree of a vertex in an undirected graph is the number of edges

incident with it

� a self-loop counts twice (both ends count)

� denoted with deg(v)

CS 5002: Discrete Math ©Northeastern University Fall 2018 98

Page 99: Lecture 12: Introduction to Graphs and Trees

Terminology: Directed Graph

� Vertex u is adjacent to vertex v in a directed graph G if (u, v) is anedge in G

� vertex u is the initial vertex of (u, v)

� Vertex v is adjacent from vertex u

� vertex v is the terminal (or end) vertex of (u, v)� Degree

� in-degree is the number of edges with the vertex as the terminal vertex

� out-degree is the number of edges with the vertex as the initial vertex

CS 5002: Discrete Math ©Northeastern University Fall 2018 99

Page 100: Lecture 12: Introduction to Graphs and Trees

CS 5002: Discrete Math ©Northeastern University Fall 2018 100

Page 101: Lecture 12: Introduction to Graphs and Trees

Kinds of Graphs

� directed vs undirected

� weighted vs unweighted

� simple vs non-simple

� sparse vs dense

� cyclic vs acyclic

� labeled vs unlabeled

CS 5002: Discrete Math ©Northeastern University Fall 2018 101

Page 102: Lecture 12: Introduction to Graphs and Trees

Directed vs Undirected

� Undirected if edge (x, y) implies

edge (y, x).

� otherwise directed

� Roads between cities are

usually undirected (go both

ways)

� Streets in cities tend to be

directed (one-way)

A

B

C

D

E

F

A

B

C

D

E

F

CS 5002: Discrete Math ©Northeastern University Fall 2018 102

Page 103: Lecture 12: Introduction to Graphs and Trees

Weighted vs Unweighted

� Each edge or vertex is assigned

a numerical value (weight).

� A road network might be

labeled with:

� length

� drive-time

� speed-limit

� In an unweighted graph, there

is no distinction between edges.

A

B

C

D

E

F

15

7

8

2

20

6

4

10

CS 5002: Discrete Math ©Northeastern University Fall 2018 103

Page 104: Lecture 12: Introduction to Graphs and Trees

Simple vs Not simple

� Some kinds of edges make

working with graphs

complicated

� A self-loop is an edge (x, x)(one vertex).

� An edge (x, y) is a multiedgeif it occurs more than once in a

graph.

A

B

C

D

E

F

CS 5002: Discrete Math ©Northeastern University Fall 2018 104

Page 105: Lecture 12: Introduction to Graphs and Trees

Sparse vs Dense

� Graphs are sparse when a small

fraction of vertex pairs have

edges between them

� Graphs are dense when a large

fraction of vertex pairs have

edges

� There’s no formal distinction

between sparse and dense

A

B

C

D

E

F

CS 5002: Discrete Math ©Northeastern University Fall 2018 105

Page 106: Lecture 12: Introduction to Graphs and Trees

Cyclic vs Acyclic

� An acyclic graph contains no

cycles

� A cyclic graph contains a cycle

� Trees are connected, acyclic,undirected graphs

� Directed acyclic graphs are

called DAGs

A

B

C

D

E

F

A

B

C

D

E

F

CS 5002: Discrete Math ©Northeastern University Fall 2018 106

Page 107: Lecture 12: Introduction to Graphs and Trees

Labeled vs Unlabeled

� Each vertex is assigned a

unique name or identifier in a

labeled graph

� In an unlabeled graph, there

are no named nodes

� Graphs usually have names—

e.g., city names in a

transportation network

� We might ignore names in

graphs to determine if they are

isomorphic (similar in structure)

CS 5002: Discrete Math ©Northeastern University Fall 2018 107

Page 108: Lecture 12: Introduction to Graphs and Trees

Section 7

Graph Representations

CS 5002: Discrete Math ©Northeastern University Fall 2018 108

Page 109: Lecture 12: Introduction to Graphs and Trees

Graph Representations

Two ways to represent a graph in code:

� Adjacency List� A list of nodes

� Every node has a list of adjacent nodes

� Adjacency Matrix� A matrix has a column and a row to represent every node

� All entries are 0 by default

� An entry G[u, v] is 1 if there is an edge from node u to v

CS 5002: Discrete Math ©Northeastern University Fall 2018 109

Page 110: Lecture 12: Introduction to Graphs and Trees

Adjacency List

For each v in V , L(v) = list of w such that (v, w) is in E:

A

B

C

D

E

F

Storage space:a|V |+ b|E|

a = sizeof(node)b = sizeof( linked list element)

CS 5002: Discrete Math ©Northeastern University Fall 2018 110

Page 111: Lecture 12: Introduction to Graphs and Trees

Adjacency Matrix

A

B

C

D

E

F

Storage space: |V |2

Does this matrix represent a directed or undirected graph?

CS 5002: Discrete Math ©Northeastern University Fall 2018 111

Page 112: Lecture 12: Introduction to Graphs and Trees

Adjacency Matrix

A

B

C

D

E

F

Storage space: |V |2

Does this matrix represent a directed or undirected graph?

CS 5002: Discrete Math ©Northeastern University Fall 2018 112

Page 113: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 113

Page 114: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 114

Page 115: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 115

Page 116: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 116

Page 117: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 117

Page 118: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 118

Page 119: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 119

Page 120: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 120

Page 121: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 121

Page 122: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 122

Page 123: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 123

Page 124: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 124

Page 125: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 125

Page 126: Lecture 12: Introduction to Graphs and Trees

Comparing Matrix vs List

1 Faster to test if (x, y) is in a

graph?

2 Faster to find the degree of a

vertex?

3 Less memory on small graphs?

4 Less memory on big graphs?

5 Edge insertion or deletion?

6 Faster to traverse the graph?

7 Be�er for most problems?

1 adjacency matrix

2 adjacency list

3 adjacency list (m+n) vs (n2)

4 adjacency matrices (a li�le)

5 adjacency matrices O(1) vs O(d)

6 adjacency list

7 adjacency list

CS 5002: Discrete Math ©Northeastern University Fall 2018 126

Page 127: Lecture 12: Introduction to Graphs and Trees

Analyzing Graph Algorithms

� Space and time are analyzed in terms of:

� Number of verticesm = |V |� Number of edges n = |E|

� Aim for polynomial running times.

� But: is O(m2) or O(n3) a be�er running time?

� depends on what the relation is between n and m

� the number of edges m can be at most n2 ≤ n2.

� connected graphs have at leastm ≥ n− 1 edges

� Stil do not know which of two running times (such asm2and n3

) are

be�er,

� Goal: implement the basic graph search algorithms in time O(m+ n).

� This is linear time, since it takes O(m+n) time simply to read the input.

� Note that when we work with connected graphs, a running time of

O(m+ n) is the same as O(m), since m ≥ n− 1.

CS 5002: Discrete Math ©Northeastern University Fall 2018 127

Page 128: Lecture 12: Introduction to Graphs and Trees

Section 8

Graph Traversals

CS 5002: Discrete Math ©Northeastern University Fall 2018 128

Page 129: Lecture 12: Introduction to Graphs and Trees

Graph Traversals

Two basic traversals:

� Breadth First Search (BFS)

� Depth First Search (DFS)

CS 5002: Discrete Math ©Northeastern University Fall 2018 129

Page 130: Lecture 12: Introduction to Graphs and Trees

BFS

Example…

CS 5002: Discrete Math ©Northeastern University Fall 2018 130

Page 131: Lecture 12: Introduction to Graphs and Trees

Green

Lake

Green

Lake

Green

Lake

UW

UWUW

Northeastern

University

Northeastern

University

Northeastern

University

Capitol

Hill

Capitol

Hill

Capitol

Hill

Fremont

FremontFremont

Space

Needle

Space

Needle

What’s the best way for me to get from Green Lake to Space Needle?

CS 5002: Discrete Math ©Northeastern University Fall 2018 131

Page 132: Lecture 12: Introduction to Graphs and Trees

Green

Lake

Green

Lake

Green

Lake

UW

UWUW

Northeastern

University

Northeastern

University

Northeastern

University

Capitol

Hill

Capitol

Hill

Capitol

Hill

Fremont

FremontFremont

Space

Needle

Space

Needle

What’s the best way for me to get from Green Lake to Space Needle?

CS 5002: Discrete Math ©Northeastern University Fall 2018 132

Page 133: Lecture 12: Introduction to Graphs and Trees

Green

Lake

Green

Lake

Green

Lake

UWUW

UW

Northeastern

University

Northeastern

University

Northeastern

University

Capitol

Hill

Capitol

Hill

Capitol

Hill

Fremont

FremontFremont

Space

Needle

Space

Needle

What’s the best way for me to get from Green Lake to Space Needle?

CS 5002: Discrete Math ©Northeastern University Fall 2018 133

Page 134: Lecture 12: Introduction to Graphs and Trees

Green

Lake

Green

Lake

Green

Lake

UW

UW

UW

Northeastern

University

Northeastern

University

Northeastern

University

Capitol

Hill

Capitol

Hill

Capitol

Hill

FremontFremont

Fremont

Space

Needle

Space

Needle

What’s the best way for me to get from Green Lake to Space Needle?

CS 5002: Discrete Math ©Northeastern University Fall 2018 134

Page 135: Lecture 12: Introduction to Graphs and Trees

Green

Lake

Green

Lake

Green

Lake

UW

UW

UW

Northeastern

University

Northeastern

University

Northeastern

University

Capitol

Hill

Capitol

Hill

Capitol

Hill

Fremont

Fremont

Fremont

Space

Needle

Space

Needle

What’s the best way for me to get from Green Lake to Space Needle?

CS 5002: Discrete Math ©Northeastern University Fall 2018 135

Page 136: Lecture 12: Introduction to Graphs and Trees

BFS: The Algorithm

� Start at the start.

� Look at all the neighbors. Are any of them the destination?

� If no:

� Look at all the neighbors of the neighbors. Are any of them the

destination?

� Look at all the neighbors of the neighbors of the neighbors. Are any of

them the destination?

CS 5002: Discrete Math ©Northeastern University Fall 2018 136

Page 137: Lecture 12: Introduction to Graphs and Trees

BFS: Runtime

� If you search the entire network, you traverse each edge at least once:

O(|E|)� That is, O(number of edges)

� Keeping a queue of who to visit in order.

� Add single node to queue: O(1)� For all nodes: O(number of nodes)

� O(|V |)� Together, it’s O(V + E)

CS 5002: Discrete Math ©Northeastern University Fall 2018 137

Page 138: Lecture 12: Introduction to Graphs and Trees

Using

� Assuming we can add and remove from our “pending” DS in O(1) time,

the entire traversal is O(|E|)� Traversal order depends on what we use for our pending DS.

� Stack : DFS

� �eue: BFS

� These are the main traversal techniques in CS, but there are others!

CS 5002: Discrete Math ©Northeastern University Fall 2018 138

Page 139: Lecture 12: Introduction to Graphs and Trees

� Depth first search needs to check which nodes have been output or

else it can get stuck in loops.

� In a connected graph, a BFS will print all nodes, but it will repeat if

there are cycles and may not terminate

� As an aside, in-order, pre-order and postorder traversals only make

sense in binary trees, so they aren’t important for graphs. However, we

do need some way to order our out-vertices (le� and right in BST).

CS 5002: Discrete Math ©Northeastern University Fall 2018 139

Page 140: Lecture 12: Introduction to Graphs and Trees

� Breadth-first always finds shortest length paths, i.e., “optimal

solutions”

� Be�er for “what is the shortest path from x to y”

� But depth-first can use less space in finding a path

� If longest path in the graph is p and highest out- degree is d then DFS

stack never has more than d ∗ p elements

� But a queue for BFS may hold O(|V |) nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 140

Page 141: Lecture 12: Introduction to Graphs and Trees

BFS vs DFS: Problems

BFS Applications

� Connected components

� Two-coloring graphs

DFS Applications

� Finding cycles

� Topological Sorting

� Strongly Connected

Components

CS 5002: Discrete Math ©Northeastern University Fall 2018 141

Page 142: Lecture 12: Introduction to Graphs and Trees

Section 9

Path Finding in a Graph

CS 5002: Discrete Math ©Northeastern University Fall 2018 142

Page 143: Lecture 12: Introduction to Graphs and Trees

Single-Source Shortest Path

Input Directed graph with non-negative weighted edges, a starting

node s and a destination node d

Problem Starting at the given node s, find the path with the lowest

total edge weight to node d

Example A map with cities as nodes and the edges are distances

between the cities. Find the shortest distance between city 1

and city 2.

CS 5002: Discrete Math ©Northeastern University Fall 2018 143

Page 144: Lecture 12: Introduction to Graphs and Trees

Djikstra’s Algorithm: Overview

� Find the “cheapest” node— the node you can get to in the shortest

amount of time.

� Update the costs of the neighbors of this node.

� Repeat until you’ve done this for each node.

� Calculate the final path.

CS 5002: Discrete Math ©Northeastern University Fall 2018 144

Page 145: Lecture 12: Introduction to Graphs and Trees

Djikstra’s Algorithm: Formally

DJIKSTRA(G,w, s)

1 INITIALIZE-SINGLE-SOURCE(G, s)2 S = ∅3 Q = G.V4 while Q 6= ∅5 u = Extract-min(Q)

6 S = S ∪ {u}7 for each vertex v ∈ G.Adj[u]8 Relax (u, v, w)

CS 5002: Discrete Math ©Northeastern University Fall 2018 145

Page 146: Lecture 12: Introduction to Graphs and Trees

Djikstra(G,w, s)

1 . G is a graph

2 . w is the weighting function such that w(u, v) returns the weight of the edge between u, v.3 . s is the starting node4 for each vertex u ∈ G5 u.d = w(s, u) . where w(s, u) =∞ if there is no edge (s, u).6 S = ∅ . Nodes we know the distance to

7 Q = G.V . min-Priority�eue starting with all our nodes, ordered by distance u.d from s found.8 while Q 6= ∅9 u = Extract-min(Q) . Greedy step: get the closest node

10 S = S ∪ {u} . Set of nodes that have shortest-path-distance found

11 for each vertex v ∈ G.Adj[u]12 Relax (u, v, w)

Relax(u, v, w)

1 . u is the start node

2 . v is the destination node

3 . w is the weight function

4 newDistance = u.d+ w(u, v)5 if newDistance < v.d6 v.d = newDistance . Priority �eue holding the nodes will be updated accordingly

CS 5002: Discrete Math ©Northeastern University Fall 2018 146

Page 147: Lecture 12: Introduction to Graphs and Trees

Djikstra’s: A walkthrough

� Find the “cheapest” node— the

node you can get to in the

shortest amount of time.

� Update the costs of the

neighbors of this node.

� Repeat until you’ve done this

for each node.

� Calculate the final path.

Breadth First Search: distance = 7

Start Finish

A

B

6

2

3

1

5

CS 5002: Discrete Math ©Northeastern University Fall 2018 147

Page 148: Lecture 12: Introduction to Graphs and Trees

Step 1: Find the cheapest node

1 Should we go to A or B?

� Make a table of how long it takes to get to each node from this node.

� We don’t know how long it takes to get to Finish, so we just say infinity

for now.

Node Time to Node

A 6

B 2

Finish ∞

Start Finish

A

B

6

2

3

1

5

CS 5002: Discrete Math ©Northeastern University Fall 2018 148

Page 149: Lecture 12: Introduction to Graphs and Trees

Step 2: Take the next step

1 Calculate how long it takes to get (from Start) to B’s neighbors by

following an edge from B

� We chose B because it’s the fastest to get to.

� Assume we started at Start, went to B, and then now we’re updating

Time to Nodes.

Node Time to Node

A �65

B 2

Finish ��∞ 7

Start Finish

A

B

6

2

3

1

5

CS 5002: Discrete Math ©Northeastern University Fall 2018 149

Page 150: Lecture 12: Introduction to Graphs and Trees

Step 3: Repeat!

1 Find the node that takes the least amount of time to get to.

� We already did B, so let’s do A.

� Update the costs of A’s neighbors

� Takes 5 to get to A; 1 more to get to Finish

Node Time to Node

A �65

B 2

Finish �76

Start Finish

A

B

6

2

3

1

5

CS 5002: Discrete Math ©Northeastern University Fall 2018 150