0% found this document useful (0 votes)
21 views

cs5002_lect12_fall18_slides (4)

Lecture 12 introduces concepts of graphs and trees in discrete mathematics, covering proof techniques such as counterexamples, induction, and loop invariants for algorithm correctness. It defines trees as directed, acyclic structures with specific terminology for nodes and edges, and discusses special types of trees like binary trees and binary search trees. The lecture emphasizes the importance of tree balance for efficient searching and the characteristics that define a valid tree.

Uploaded by

codeshacking7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

cs5002_lect12_fall18_slides (4)

Lecture 12 introduces concepts of graphs and trees in discrete mathematics, covering proof techniques such as counterexamples, induction, and loop invariants for algorithm correctness. It defines trees as directed, acyclic structures with specific terminology for nodes and edges, and discusses special types of trees like binary trees and binary search trees. The lecture emphasizes the importance of tree balance for efficient searching and the characteristics that define a valid tree.

Uploaded by

codeshacking7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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


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


Discrete and Data Structures
Outdoors Navigation Spr

astern
CS 5002:University
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 ), F

E
where v1 , v2 are vertices in V B
 V = {A, B, C, D, E, F }
 E = {(A, B), (A, D), (B, C), C

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

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 F

E
edge (y, x). B

 otherwise directed
C

D
 Roads between cities are A

usually undirected (go both F

ways) E

 Streets in cities tend to be


C
directed (one-way)
D

CS 5002: Discrete Math ©Northeastern University Fall 2018 102


Weighted vs Unweighted

 Each edge or vertex is assigned F


6
20 E
a numerical value (weight). B
10
 A road network might be 8
4

labeled with: 15 C
2
 length D

 drive-time A
7

 speed-limit
 In an unweighted graph, there
is no distinction between edges.

CS 5002: Discrete Math ©Northeastern University Fall 2018 103


Simple vs Not simple

 Some kinds of edges make F

E
working with graphs B

complicated
 A self-loop is an edge (x, x) C

(one vertex). D

 An edge (x, y) is a multiedge A

if it occurs more than once in a


graph.

CS 5002: Discrete Math ©Northeastern University Fall 2018 104


Sparse vs Dense

 Graphs are sparse when a small F

E
fraction of vertex pairs have B

edges between them


 Graphs are dense when a large C

fraction of vertex pairs have D

A
edges
 There’s no formal distinction
between sparse and dense

CS 5002: Discrete Math ©Northeastern University Fall 2018 105


Cyclic vs Acyclic

 An acyclic graph contains no F

E
cycles B

 A cyclic graph contains a cycle C

 Trees are connected, acyclic, A

undirected graphs E

 Directed acyclic graphs are


called DAGs C

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:

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

Storage space: |V |2

CS 5002: Discrete Math ©Northeastern University Fall 2018 111


Adjacency Matrix

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 1 adjacency matrix


graph?

CS 5002: Discrete Math ©Northeastern University Fall 2018 114


Comparing Matrix vs List

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


graph?
2 Faster to find the degree of a
vertex?

CS 5002: Discrete Math ©Northeastern University Fall 2018 115


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex?

CS 5002: Discrete Math ©Northeastern University Fall 2018 116


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex?
3 Less memory on small graphs?

CS 5002: Discrete Math ©Northeastern University Fall 2018 117


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs?

CS 5002: Discrete Math ©Northeastern University Fall 2018 118


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs?
4 Less memory on big graphs?

CS 5002: Discrete Math ©Northeastern University Fall 2018 119


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?

CS 5002: Discrete Math ©Northeastern University Fall 2018 120


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?
5 Edge insertion or deletion?

CS 5002: Discrete Math ©Northeastern University Fall 2018 121


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?
5 adjacency matrices O(1) vs O(d)
5 Edge insertion or deletion?

CS 5002: Discrete Math ©Northeastern University Fall 2018 122


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?
5 adjacency matrices O(1) vs O(d)
5 Edge insertion or deletion?
6 Faster to traverse the graph?

CS 5002: Discrete Math ©Northeastern University Fall 2018 123


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?
5 adjacency matrices O(1) vs O(d)
5 Edge insertion or deletion?
6 Faster to traverse the graph? 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 1 adjacency matrix


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?
5 adjacency matrices O(1) vs O(d)
5 Edge insertion or deletion?
6 Faster to traverse the graph? 6 adjacency list
7 Better for most problems?

CS 5002: Discrete Math ©Northeastern University Fall 2018 125


Comparing Matrix vs List

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


graph? 2 adjacency list
2 Faster to find the degree of a
vertex? 3 adjacency list (m+n) vs (n2)
3 Less memory on small graphs? 4 adjacency matrices (a little)
4 Less memory on big graphs?
5 adjacency matrices O(1) vs O(d)
5 Edge insertion or deletion?
6 Faster to traverse the graph? 6 adjacency list
7 Better for most problems? 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
UW
Lake

Fremont
Capitol
Hill

Northeastern
University
Space
Needle

CS 5002: Discrete Math ©Northeastern University Fall 2018 131


Green
UW
Lake

Fremont
Capitol
Hill

Northeastern
University
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
UW
Lake

Fremont
Capitol
Hill

Northeastern
University
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
UW
Lake

Fremont
Capitol
Hill

Northeastern
University
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
UW
Lake

Fremont
Capitol
Hill

Northeastern
University
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 DFS Applications


 Connected components  Finding cycles
 Two-coloring graphs  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 A


node you can get to in the
6
shortest amount of time.
 Update the costs of the 1
neighbors of this node.
Start 3 Finish
 Repeat until you’ve done this
for each node.
 Calculate the final path. 2 5
B
Breadth First Search: distance = 7

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


A 6
6
B 2
Finish ∞ 1

Start 3 Finish

2 5
B

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


A 65
6
B 2
Finish ∞
7
 1

Start 3 Finish

2 5
B

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


A 65
6
B 2
Finish 76 1

Start 3 Finish

2 5
B

CS 5002: Discrete Math ©Northeastern University Fall 2018 150

You might also like