ݺߣ

ݺߣShare a Scribd company logo
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
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
Section 1
Review: Proof Techniques
CS 5002: Discrete Math ©Northeastern University Fall 2018 3
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 iffs, proof
by contradiction, proof by contrapositive
CS 5002: Discrete Math ©Northeastern University Fall 2018 4
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
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
Proof by Loop Invariant
 Built off 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
Proof by Loop Invariant Is…
Invariant: something that is always true
After 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
Steps to Loop Invariant Proof
After finding your loop invariant:
 Initialization
 Prior to the loop initiating, does the property hold?
 Maintenance
 After each loop iteration, does the property still hold, given the
initialization properties?
 Termination
 After the loop terminates, does the property still hold? And for what
data?
CS 5002: Discrete Math ©Northeastern University Fall 2018 9
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
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
Section 2
Some Graph and Tree Problems
CS 5002: Discrete Math ©Northeastern University Fall 2018 12
Outdoors Navigation
Discrete and Data Structures Spr
astern University
CS 5002: Discrete Math ©Northeastern University Fall 2018 13
Indoors Navigation
CS 5002: Discrete Math ©Northeastern University Fall 2018 14
Telecommunication Networks
CS 5002: Discrete Math ©Northeastern University Fall 2018 15
Social Networks
CS 5002: Discrete Math ©Northeastern University Fall 2018 16
Section 3
Introduction to Trees
CS 5002: Discrete Math ©Northeastern University Fall 2018 17
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
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
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
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
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
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
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
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
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
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
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
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
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
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
What is Not a Tree?
Problems:
 Cycles: there is a cycle in the tree
 Multiple parents: node 3 has multiple parents on different levels
CS 5002: Discrete Math ©Northeastern University Fall 2018 32
What is Not a Tree?
Problems:
 Cycles: there is an undirected cycle in the tree
 Multiple parents: node 5 has multiple parents on different levels
CS 5002: Discrete Math ©Northeastern University Fall 2018 33
What is Not a Tree?
Problems:
 Disconnected components: there are two unconnected groups of
nodes
CS 5002: Discrete Math ©Northeastern University Fall 2018 34
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
Section 4
Special Trees
CS 5002: Discrete Math ©Northeastern University Fall 2018 36
Special Trees
 Binary Tree
 Binary Search Tree
 Balanced Tree
 Binary Heap/Priority Queue
 Red-Black Tree
CS 5002: Discrete Math ©Northeastern University Fall 2018 37
Binary Trees
Binary tree - a tree where every node has at most two children
CS 5002: Discrete Math ©Northeastern University Fall 2018 38
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 left 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
Example: Binary Search Trees?
Binary search tree (BST) - a tree where nodes are organized in a sorted
order to make it easier to search
 Left tree is a BST
 Right tree is not a BST - node 7 is on the left hand-side of the root
node, and yet it is greater than it
CS 5002: Discrete Math ©Northeastern University Fall 2018 40
Example: Using BSTs
Suppose we want to find who has the score of 15…
CS 5002: Discrete Math ©Northeastern University Fall 2018 41
Example: Using BSTs
Suppose we want to find who has the score of 15:
 Start at the root
CS 5002: Discrete Math ©Northeastern University Fall 2018 42
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 left
CS 5002: Discrete Math ©Northeastern University Fall 2018 43
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 left
 If the score is  15, go to the right
CS 5002: Discrete Math ©Northeastern University Fall 2018 44
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 left
 If the score is  15, go to the right
 If the score == 15, stop
CS 5002: Discrete Math ©Northeastern University Fall 2018 45
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
Balanced Trees
Consider the following two trees. Which tree would it make it easier for us
to search for an element?
Observation: height is often 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
Tree Balance and Height
How do we define balance?
 If the heights of the left and right trees are balanced, the tree is
balanced, so:
CS 5002: Discrete Math ©Northeastern University Fall 2018 48
Tree Balance and Height
How do we define balance?
 If the heights of the left and right trees are balanced, the tree is
balanced, so:
|(height(left) − height(right))|
CS 5002: Discrete Math ©Northeastern University Fall 2018 49
Tree Balance and Height
How do we define balance?
 If the heights of the left and right trees are balanced, the tree is
balanced, so:
|(height(left) − height(right))|
 Anything wrong with this approach?
CS 5002: Discrete Math ©Northeastern University Fall 2018 50
Tree Balance and Height
How do we define balance?
 If the heights of the left and right trees are balanced, the tree is
balanced, so:
|(height(left) − height(right))|
 Anything wrong with this approach?
 Are these trees balanced?
CS 5002: Discrete Math ©Northeastern University Fall 2018 51
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 left and right subtrees
differ by at most 1
CS 5002: Discrete Math ©Northeastern University Fall 2018 52
Example: Balanced Trees?
 The left tree is balanced.
 The right tree is not balanced. The height difference between nodes 2
and 8 is two.
CS 5002: Discrete Math ©Northeastern University Fall 2018 53
Section 5
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 54
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
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 56
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 57
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 58
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 59
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 60
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 61
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 62
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 63
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 64
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 65
Tree Traversals
Summary:
CS 5002: Discrete Math ©Northeastern University Fall 2018 66
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 67
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 68
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
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 70
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 71
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 72
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 73
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 74
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 75
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 76
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 77
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 78
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 79
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 80
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 81
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 82
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 83
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 84
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 85
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 86
Tree Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 87
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
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
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
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
Tree Traversals Example
Traverse the tree below, using:
 Pre-order traversal: 25 – 10 – 7 – 8 – 12 – 15 – 5
CS 5002: Discrete Math ©Northeastern University Fall 2018 92
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
CS 5002: Discrete Math ©Northeastern University Fall 2018 93
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
Section 6
Introduction to Graphs
CS 5002: Discrete Math ©Northeastern University Fall 2018 95
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
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
Terminology: Undirected Graph
 Two vertices u and v are adjacent in an undirected graph G if {u, v} is
an 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
Terminology: Directed Graph
 Vertex u is adjacent to vertex v in a directed graph G if (u, v) is an
edge 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
CS 5002: Discrete Math ©Northeastern University Fall 2018 100
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
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
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
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 multiedge
if it occurs more than once in a
graph.
A
B
C
D
E
F
CS 5002: Discrete Math ©Northeastern University Fall 2018 104
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
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
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
Section 7
Graph Representations
CS 5002: Discrete Math ©Northeastern University Fall 2018 108
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
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
Adjacency Matrix
A
B
C
D
E
F
Storage space: |V |2
CS 5002: Discrete Math ©Northeastern University Fall 2018 111
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
Comparing Matrix vs List
1 Faster to test if (x, y) is in a
graph?
CS 5002: Discrete Math ©Northeastern University Fall 2018 113
Comparing Matrix vs List
1 Faster to test if (x, y) is in a
graph?
1 adjacency matrix
CS 5002: Discrete Math ©Northeastern University Fall 2018 114
Comparing Matrix vs List
1 Faster to test if (x, y) is in a
graph?
2 Faster to find the degree of a
vertex?
1 adjacency matrix
CS 5002: Discrete Math ©Northeastern University Fall 2018 115
Comparing Matrix vs List
1 Faster to test if (x, y) is in a
graph?
2 Faster to find the degree of a
vertex?
1 adjacency matrix
2 adjacency list
CS 5002: Discrete Math ©Northeastern University Fall 2018 116
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?
1 adjacency matrix
2 adjacency list
CS 5002: Discrete Math ©Northeastern University Fall 2018 117
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
CS 5002: Discrete Math ©Northeastern University Fall 2018 118
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
CS 5002: Discrete Math ©Northeastern University Fall 2018 119
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
CS 5002: Discrete Math ©Northeastern University Fall 2018 120
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
CS 5002: Discrete Math ©Northeastern University Fall 2018 121
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
5 adjacency matrices O(1) vs O(d)
CS 5002: Discrete Math ©Northeastern University Fall 2018 122
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
5 adjacency matrices O(1) vs O(d)
CS 5002: Discrete Math ©Northeastern University Fall 2018 123
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?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
5 adjacency matrices O(1) vs O(d)
6 adjacency list
CS 5002: Discrete Math ©Northeastern University Fall 2018 124
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 Better for most problems?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
5 adjacency matrices O(1) vs O(d)
6 adjacency list
CS 5002: Discrete Math ©Northeastern University Fall 2018 125
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 Better for most problems?
1 adjacency matrix
2 adjacency list
3 adjacency list (m+n) vs (n2)
4 adjacency matrices (a little)
5 adjacency matrices O(1) vs O(d)
6 adjacency list
7 adjacency list
CS 5002: Discrete Math ©Northeastern University Fall 2018 126
Analyzing Graph Algorithms
 Space and time are analyzed in terms of:
 Number of vertices m = |V |
 Number of edges n = |E|
 Aim for polynomial running times.
 But: is O(m2) or O(n3) a better 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 least m ≥ n − 1 edges
 Stil do not know which of two running times (such as m2 and n3) are
better,
 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
Section 8
Graph Traversals
CS 5002: Discrete Math ©Northeastern University Fall 2018 128
Graph Traversals
Two basic traversals:
 Breadth First Search (BFS)
 Depth First Search (DFS)
CS 5002: Discrete Math ©Northeastern University Fall 2018 129
BFS
Example…
CS 5002: Discrete Math ©Northeastern University Fall 2018 130
Green
Lake
UW
Northeastern
University
Capitol
Hill
Fremont
Space
Needle
CS 5002: Discrete Math ©Northeastern University Fall 2018 131
Green
Lake
Green
Lake
UW
Northeastern
University
Capitol
Hill
Fremont
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
Green
Lake
Green
Lake
UW
UW
Northeastern
University
Northeastern
University
Capitol
Hill
Fremont
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
Green
Lake
Green
Lake
UW
UW
Northeastern
University
Northeastern
University
Capitol
Hill
Capitol
Hill
Fremont
Fremont
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
Green
Lake
Green
Lake
UW
UW
Northeastern
University
Northeastern
University
Capitol
Hill
Capitol
Hill
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
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
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
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
 Queue: BFS
 These are the main traversal techniques in CS, but there are others!
CS 5002: Discrete Math ©Northeastern University Fall 2018 138
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 (left and right in BST).
CS 5002: Discrete Math ©Northeastern University Fall 2018 139
Breadth-first always finds shortest length paths, i.e., “optimal
solutions”
 Better 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
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
Section 9
Path Finding in a Graph
CS 5002: Discrete Math ©Northeastern University Fall 2018 142
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
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
Djikstra’s Algorithm: Formally
DJIKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S = ∅
3 Q = G.V
4 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
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 e
3 . s is the starting node
4 for each vertex u ∈ G
5 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-PriorityQueue starting with all our nodes, ordered by dist
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)
CS 5002: Discrete Math ©Northeastern University Fall 2018 146
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
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
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
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

More Related Content

Similar to cs5002_lect12_fall18_slides graphs and trees (7)

Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
Mukul Kumar Singh Chauhan
Introduction to data mining and machine learning
Introduction to data mining and machine learningIntroduction to data mining and machine learning
Introduction to data mining and machine learning
Tilani Gunawardena PhD(UNIBAS), BSc(Pera), FHEA(UK), CEng, MIESL
Classification & Clustering.pptx
Classification & Clustering.pptxClassification & Clustering.pptx
Classification & Clustering.pptx
ImXaib
Higher-order clustering coefficients
Higher-order clustering coefficientsHigher-order clustering coefficients
Higher-order clustering coefficients
Austin Benson
Algorithms
AlgorithmsAlgorithms
Algorithms
DrHiyamHatem
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Xiaohan Zeng
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion

More from hassanbokhari14 (11)

An introduction to the 6th sense technology.pptx
An introduction to the 6th sense technology.pptxAn introduction to the 6th sense technology.pptx
An introduction to the 6th sense technology.pptx
hassanbokhari14
An introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematicsAn introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematics
hassanbokhari14
An introduction to the concepts of Mathematical Induction.pdf
An introduction to the concepts of Mathematical Induction.pdfAn introduction to the concepts of Mathematical Induction.pdf
An introduction to the concepts of Mathematical Induction.pdf
hassanbokhari14
Programming Development LifeCycle11.pptx
Programming Development LifeCycle11.pptxProgramming Development LifeCycle11.pptx
Programming Development LifeCycle11.pptx
hassanbokhari14
ICT Lecture 2 Components of Computer.ppt
ICT Lecture 2 Components of Computer.pptICT Lecture 2 Components of Computer.ppt
ICT Lecture 2 Components of Computer.ppt
hassanbokhari14
An introduction to the computer basics explained
An introduction to the computer basics explainedAn introduction to the computer basics explained
An introduction to the computer basics explained
hassanbokhari14
An introduction to the different number systems
An introduction to the different number systemsAn introduction to the different number systems
An introduction to the different number systems
hassanbokhari14
An introduction to the program development lifecycle
An introduction to the program development lifecycleAn introduction to the program development lifecycle
An introduction to the program development lifecycle
hassanbokhari14
INTRODUCTION_TO_COMPUTER_NETWORKS for students
INTRODUCTION_TO_COMPUTER_NETWORKS for studentsINTRODUCTION_TO_COMPUTER_NETWORKS for students
INTRODUCTION_TO_COMPUTER_NETWORKS for students
hassanbokhari14
An introduction to the Microsoft Excel 2010
An introduction to the Microsoft Excel 2010An introduction to the Microsoft Excel 2010
An introduction to the Microsoft Excel 2010
hassanbokhari14
AI and Deep learning in health care.pptx
AI and Deep learning in health care.pptxAI and Deep learning in health care.pptx
AI and Deep learning in health care.pptx
hassanbokhari14
An introduction to the 6th sense technology.pptx
An introduction to the 6th sense technology.pptxAn introduction to the 6th sense technology.pptx
An introduction to the 6th sense technology.pptx
hassanbokhari14
An introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematicsAn introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematics
hassanbokhari14
An introduction to the concepts of Mathematical Induction.pdf
An introduction to the concepts of Mathematical Induction.pdfAn introduction to the concepts of Mathematical Induction.pdf
An introduction to the concepts of Mathematical Induction.pdf
hassanbokhari14
Programming Development LifeCycle11.pptx
Programming Development LifeCycle11.pptxProgramming Development LifeCycle11.pptx
Programming Development LifeCycle11.pptx
hassanbokhari14
ICT Lecture 2 Components of Computer.ppt
ICT Lecture 2 Components of Computer.pptICT Lecture 2 Components of Computer.ppt
ICT Lecture 2 Components of Computer.ppt
hassanbokhari14
An introduction to the computer basics explained
An introduction to the computer basics explainedAn introduction to the computer basics explained
An introduction to the computer basics explained
hassanbokhari14
An introduction to the different number systems
An introduction to the different number systemsAn introduction to the different number systems
An introduction to the different number systems
hassanbokhari14
An introduction to the program development lifecycle
An introduction to the program development lifecycleAn introduction to the program development lifecycle
An introduction to the program development lifecycle
hassanbokhari14
INTRODUCTION_TO_COMPUTER_NETWORKS for students
INTRODUCTION_TO_COMPUTER_NETWORKS for studentsINTRODUCTION_TO_COMPUTER_NETWORKS for students
INTRODUCTION_TO_COMPUTER_NETWORKS for students
hassanbokhari14
An introduction to the Microsoft Excel 2010
An introduction to the Microsoft Excel 2010An introduction to the Microsoft Excel 2010
An introduction to the Microsoft Excel 2010
hassanbokhari14
AI and Deep learning in health care.pptx
AI and Deep learning in health care.pptxAI and Deep learning in health care.pptx
AI and Deep learning in health care.pptx
hassanbokhari14

Recently uploaded (20)

TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
The Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, Tulu
The Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, TuluThe Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, Tulu
The Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, Tulu
DrIArulAram
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Useful environment methods in Odoo 18 - Odoo ݺߣs
Useful environment methods in Odoo 18 - Odoo ݺߣsUseful environment methods in Odoo 18 - Odoo ݺߣs
Useful environment methods in Odoo 18 - Odoo ݺߣs
Celine George
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18
Celine George
How to Setup WhatsApp in Odoo 17 - Odoo ݺߣs
How to Setup WhatsApp in Odoo 17 - Odoo ݺߣsHow to Setup WhatsApp in Odoo 17 - Odoo ݺߣs
How to Setup WhatsApp in Odoo 17 - Odoo ݺߣs
Celine George
How to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of SaleHow to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of Sale
Celine George
Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
A PPT Presentation on The Princess and the God: A tale of ancient India  by A...A PPT Presentation on The Princess and the God: A tale of ancient India  by A...
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
Beena E S
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
DUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAM
DUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAMDUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAM
DUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAM
vlckovar
The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .
saanidhyapatel09
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
pinkdvil200
Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Database population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slidesDatabase population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slides
Celine George
A PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of FireA PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of Fire
Beena E S
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptxCRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
PoojaSen20
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
The Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, Tulu
The Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, TuluThe Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, Tulu
The Dravidian Languages: Tamil, Telugu, Kannada, Malayalam, Brahui, Kuvi, Tulu
DrIArulAram
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Useful environment methods in Odoo 18 - Odoo ݺߣs
Useful environment methods in Odoo 18 - Odoo ݺߣsUseful environment methods in Odoo 18 - Odoo ݺߣs
Useful environment methods in Odoo 18 - Odoo ݺߣs
Celine George
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18
Celine George
How to Setup WhatsApp in Odoo 17 - Odoo ݺߣs
How to Setup WhatsApp in Odoo 17 - Odoo ݺߣsHow to Setup WhatsApp in Odoo 17 - Odoo ݺߣs
How to Setup WhatsApp in Odoo 17 - Odoo ݺߣs
Celine George
How to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of SaleHow to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of Sale
Celine George
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
A PPT Presentation on The Princess and the God: A tale of ancient India  by A...A PPT Presentation on The Princess and the God: A tale of ancient India  by A...
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
Beena E S
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
DUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAM
DUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAMDUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAM
DUBLIN PROGRAM DUBLIN PROGRAM DUBLIN PROGRAM
vlckovar
The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .The Constitution, Government and Law making bodies .
The Constitution, Government and Law making bodies .
saanidhyapatel09
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
pinkdvil200
Database population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slidesDatabase population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slides
Celine George
A PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of FireA PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of Fire
Beena E S
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptxCRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
PoojaSen20

cs5002_lect12_fall18_slides graphs and trees

  • 1. 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
  • 2. 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
  • 3. Section 1 Review: Proof Techniques CS 5002: Discrete Math ©Northeastern University Fall 2018 3
  • 4. 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 iffs, proof by contradiction, proof by contrapositive CS 5002: Discrete Math ©Northeastern University Fall 2018 4
  • 5. 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
  • 6. 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
  • 7. Proof by Loop Invariant Built off 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
  • 8. Proof by Loop Invariant Is… Invariant: something that is always true After 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
  • 9. Steps to Loop Invariant Proof After finding your loop invariant: Initialization Prior to the loop initiating, does the property hold? Maintenance After each loop iteration, does the property still hold, given the initialization properties? Termination After the loop terminates, does the property still hold? And for what data? CS 5002: Discrete Math ©Northeastern University Fall 2018 9
  • 10. 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
  • 11. 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
  • 12. Section 2 Some Graph and Tree Problems CS 5002: Discrete Math ©Northeastern University Fall 2018 12
  • 13. Outdoors Navigation Discrete and Data Structures Spr astern University CS 5002: Discrete Math ©Northeastern University Fall 2018 13
  • 14. Indoors Navigation CS 5002: Discrete Math ©Northeastern University Fall 2018 14
  • 15. Telecommunication Networks CS 5002: Discrete Math ©Northeastern University Fall 2018 15
  • 16. Social Networks CS 5002: Discrete Math ©Northeastern University Fall 2018 16
  • 17. Section 3 Introduction to Trees CS 5002: Discrete Math ©Northeastern University Fall 2018 17
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. What is Not a Tree? Problems: Cycles: there is a cycle in the tree Multiple parents: node 3 has multiple parents on different levels CS 5002: Discrete Math ©Northeastern University Fall 2018 32
  • 33. What is Not a Tree? Problems: Cycles: there is an undirected cycle in the tree Multiple parents: node 5 has multiple parents on different levels CS 5002: Discrete Math ©Northeastern University Fall 2018 33
  • 34. What is Not a Tree? Problems: Disconnected components: there are two unconnected groups of nodes CS 5002: Discrete Math ©Northeastern University Fall 2018 34
  • 35. 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
  • 36. Section 4 Special Trees CS 5002: Discrete Math ©Northeastern University Fall 2018 36
  • 37. Special Trees Binary Tree Binary Search Tree Balanced Tree Binary Heap/Priority Queue Red-Black Tree CS 5002: Discrete Math ©Northeastern University Fall 2018 37
  • 38. Binary Trees Binary tree - a tree where every node has at most two children CS 5002: Discrete Math ©Northeastern University Fall 2018 38
  • 39. 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 left 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
  • 40. Example: Binary Search Trees? Binary search tree (BST) - a tree where nodes are organized in a sorted order to make it easier to search Left tree is a BST Right tree is not a BST - node 7 is on the left hand-side of the root node, and yet it is greater than it CS 5002: Discrete Math ©Northeastern University Fall 2018 40
  • 41. Example: Using BSTs Suppose we want to find who has the score of 15… CS 5002: Discrete Math ©Northeastern University Fall 2018 41
  • 42. Example: Using BSTs Suppose we want to find who has the score of 15: Start at the root CS 5002: Discrete Math ©Northeastern University Fall 2018 42
  • 43. 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 left CS 5002: Discrete Math ©Northeastern University Fall 2018 43
  • 44. 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 left If the score is 15, go to the right CS 5002: Discrete Math ©Northeastern University Fall 2018 44
  • 45. 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 left If the score is 15, go to the right If the score == 15, stop CS 5002: Discrete Math ©Northeastern University Fall 2018 45
  • 46. 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
  • 47. Balanced Trees Consider the following two trees. Which tree would it make it easier for us to search for an element? Observation: height is often 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
  • 48. Tree Balance and Height How do we define balance? If the heights of the left and right trees are balanced, the tree is balanced, so: CS 5002: Discrete Math ©Northeastern University Fall 2018 48
  • 49. Tree Balance and Height How do we define balance? If the heights of the left and right trees are balanced, the tree is balanced, so: |(height(left) − height(right))| CS 5002: Discrete Math ©Northeastern University Fall 2018 49
  • 50. Tree Balance and Height How do we define balance? If the heights of the left and right trees are balanced, the tree is balanced, so: |(height(left) − height(right))| Anything wrong with this approach? CS 5002: Discrete Math ©Northeastern University Fall 2018 50
  • 51. Tree Balance and Height How do we define balance? If the heights of the left and right trees are balanced, the tree is balanced, so: |(height(left) − height(right))| Anything wrong with this approach? Are these trees balanced? CS 5002: Discrete Math ©Northeastern University Fall 2018 51
  • 52. 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 left and right subtrees differ by at most 1 CS 5002: Discrete Math ©Northeastern University Fall 2018 52
  • 53. Example: Balanced Trees? The left tree is balanced. The right tree is not balanced. The height difference between nodes 2 and 8 is two. CS 5002: Discrete Math ©Northeastern University Fall 2018 53
  • 54. Section 5 Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 54
  • 55. 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
  • 56. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 56
  • 57. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 57
  • 58. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 58
  • 59. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 59
  • 60. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 60
  • 61. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 61
  • 62. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 62
  • 63. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 63
  • 64. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 64
  • 65. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 65
  • 66. Tree Traversals Summary: CS 5002: Discrete Math ©Northeastern University Fall 2018 66
  • 67. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 67
  • 68. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 68
  • 69. 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
  • 70. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 70
  • 71. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 71
  • 72. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 72
  • 73. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 73
  • 74. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 74
  • 75. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 75
  • 76. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 76
  • 77. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 77
  • 78. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 78
  • 79. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 79
  • 80. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 80
  • 81. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 81
  • 82. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 82
  • 83. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 83
  • 84. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 84
  • 85. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 85
  • 86. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 86
  • 87. Tree Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 87
  • 88. 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
  • 89. 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
  • 90. 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
  • 91. 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
  • 92. Tree Traversals Example Traverse the tree below, using: Pre-order traversal: 25 – 10 – 7 – 8 – 12 – 15 – 5 CS 5002: Discrete Math ©Northeastern University Fall 2018 92
  • 93. 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 CS 5002: Discrete Math ©Northeastern University Fall 2018 93
  • 94. 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
  • 95. Section 6 Introduction to Graphs CS 5002: Discrete Math ©Northeastern University Fall 2018 95
  • 96. 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
  • 97. 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
  • 98. Terminology: Undirected Graph Two vertices u and v are adjacent in an undirected graph G if {u, v} is an 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
  • 99. Terminology: Directed Graph Vertex u is adjacent to vertex v in a directed graph G if (u, v) is an edge 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
  • 100. CS 5002: Discrete Math ©Northeastern University Fall 2018 100
  • 101. 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
  • 102. 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
  • 103. 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
  • 104. 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 multiedge if it occurs more than once in a graph. A B C D E F CS 5002: Discrete Math ©Northeastern University Fall 2018 104
  • 105. 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
  • 106. 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
  • 107. 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
  • 108. Section 7 Graph Representations CS 5002: Discrete Math ©Northeastern University Fall 2018 108
  • 109. 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
  • 110. 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
  • 111. Adjacency Matrix A B C D E F Storage space: |V |2 CS 5002: Discrete Math ©Northeastern University Fall 2018 111
  • 112. 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
  • 113. Comparing Matrix vs List 1 Faster to test if (x, y) is in a graph? CS 5002: Discrete Math ©Northeastern University Fall 2018 113
  • 114. Comparing Matrix vs List 1 Faster to test if (x, y) is in a graph? 1 adjacency matrix CS 5002: Discrete Math ©Northeastern University Fall 2018 114
  • 115. Comparing Matrix vs List 1 Faster to test if (x, y) is in a graph? 2 Faster to find the degree of a vertex? 1 adjacency matrix CS 5002: Discrete Math ©Northeastern University Fall 2018 115
  • 116. Comparing Matrix vs List 1 Faster to test if (x, y) is in a graph? 2 Faster to find the degree of a vertex? 1 adjacency matrix 2 adjacency list CS 5002: Discrete Math ©Northeastern University Fall 2018 116
  • 117. 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? 1 adjacency matrix 2 adjacency list CS 5002: Discrete Math ©Northeastern University Fall 2018 117
  • 118. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) CS 5002: Discrete Math ©Northeastern University Fall 2018 118
  • 119. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) CS 5002: Discrete Math ©Northeastern University Fall 2018 119
  • 120. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) CS 5002: Discrete Math ©Northeastern University Fall 2018 120
  • 121. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) CS 5002: Discrete Math ©Northeastern University Fall 2018 121
  • 122. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) 5 adjacency matrices O(1) vs O(d) CS 5002: Discrete Math ©Northeastern University Fall 2018 122
  • 123. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) 5 adjacency matrices O(1) vs O(d) CS 5002: Discrete Math ©Northeastern University Fall 2018 123
  • 124. 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? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) 5 adjacency matrices O(1) vs O(d) 6 adjacency list CS 5002: Discrete Math ©Northeastern University Fall 2018 124
  • 125. 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 Better for most problems? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) 5 adjacency matrices O(1) vs O(d) 6 adjacency list CS 5002: Discrete Math ©Northeastern University Fall 2018 125
  • 126. 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 Better for most problems? 1 adjacency matrix 2 adjacency list 3 adjacency list (m+n) vs (n2) 4 adjacency matrices (a little) 5 adjacency matrices O(1) vs O(d) 6 adjacency list 7 adjacency list CS 5002: Discrete Math ©Northeastern University Fall 2018 126
  • 127. Analyzing Graph Algorithms Space and time are analyzed in terms of: Number of vertices m = |V | Number of edges n = |E| Aim for polynomial running times. But: is O(m2) or O(n3) a better 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 least m ≥ n − 1 edges Stil do not know which of two running times (such as m2 and n3) are better, 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
  • 128. Section 8 Graph Traversals CS 5002: Discrete Math ©Northeastern University Fall 2018 128
  • 129. Graph Traversals Two basic traversals: Breadth First Search (BFS) Depth First Search (DFS) CS 5002: Discrete Math ©Northeastern University Fall 2018 129
  • 130. BFS Example… CS 5002: Discrete Math ©Northeastern University Fall 2018 130
  • 132. Green Lake Green Lake UW Northeastern University Capitol Hill Fremont 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
  • 133. Green Lake Green Lake UW UW Northeastern University Northeastern University Capitol Hill Fremont 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
  • 134. Green Lake Green Lake UW UW Northeastern University Northeastern University Capitol Hill Capitol Hill Fremont Fremont 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
  • 135. Green Lake Green Lake UW UW Northeastern University Northeastern University Capitol Hill Capitol Hill 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
  • 136. 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
  • 137. 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
  • 138. 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 Queue: BFS These are the main traversal techniques in CS, but there are others! CS 5002: Discrete Math ©Northeastern University Fall 2018 138
  • 139. 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 (left and right in BST). CS 5002: Discrete Math ©Northeastern University Fall 2018 139
  • 140. Breadth-first always finds shortest length paths, i.e., “optimal solutions” Better 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
  • 141. 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
  • 142. Section 9 Path Finding in a Graph CS 5002: Discrete Math ©Northeastern University Fall 2018 142
  • 143. 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
  • 144. 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
  • 145. Djikstra’s Algorithm: Formally DJIKSTRA(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S = ∅ 3 Q = G.V 4 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
  • 146. 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 e 3 . s is the starting node 4 for each vertex u ∈ G 5 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-PriorityQueue starting with all our nodes, ordered by dist 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) CS 5002: Discrete Math ©Northeastern University Fall 2018 146
  • 147. 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
  • 148. 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
  • 149. 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
  • 150. 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