cs5002_lect12_fall18_slides (4)
cs5002_lect12_fall18_slides (4)
Northeastern University
3 Introduction to Trees
4 Special Trees
5 Tree Traversals
6 Introduction to Graphs
7 Graph Representations
8 Graph Traversals
Proof by:
astern
CS 5002:University
Discrete Math ©Northeastern University Fall 2018 13
Indoors Navigation
Introduction to Trees
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
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.
Problems:
Cycles: there is a cycle in the tree
Multiple parents: node 3 has multiple parents on different levels
Special Trees
Binary Tree
Binary Search Tree
Balanced Tree
Binary Heap/Priority Queue
Red-Black Tree
Binary tree - a tree where every node has at most two children
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
Consider the following two trees. Which tree would it make it easier for us
to search for an element?
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.
Tree Traversals
Summary:
25 – 10 – 12 – 7 – 8 – 15 – 5
CS 5002: Discrete Math ©Northeastern University Fall 2018 89
DFS Example
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
Introduction to Graphs
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
E
where v1 , v2 are vertices in V B
V = {A, B, C, D, E, F }
E = {(A, B), (A, D), (B, C), C
directed vs undirected
weighted vs unweighted
simple vs non-simple
sparse vs dense
cyclic vs acyclic
labeled vs unlabeled
E
edge (y, x). B
otherwise directed
C
D
Roads between cities are A
ways) E
labeled with: 15 C
2
length D
drive-time A
7
speed-limit
In an unweighted graph, there
is no distinction between edges.
E
working with graphs B
complicated
A self-loop is an edge (x, x) C
(one vertex). D
E
fraction of vertex pairs have B
A
edges
There’s no formal distinction
between sparse and dense
E
cycles B
undirected graphs E
Graph Representations
Storage space:
a|V | + b|E|
a = sizeof(node)
b = sizeof( linked list element)
Storage space: |V |2
Storage space: |V |2
Graph Traversals
Example…
Fremont
Capitol
Hill
Northeastern
University
Space
Needle
Fremont
Capitol
Hill
Northeastern
University
Space
Needle
What’s the best way for me to get from Green Lake to Space Needle?
Fremont
Capitol
Hill
Northeastern
University
Space
Needle
What’s the best way for me to get from Green Lake to Space Needle?
Fremont
Capitol
Hill
Northeastern
University
Space
Needle
What’s the best way for me to get from Green Lake to Space Needle?
Fremont
Capitol
Hill
Northeastern
University
Space
Needle
What’s the best way for me to get from Green Lake to Space Needle?
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)
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!
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.
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)
Start 3 Finish
2 5
B
Start 3 Finish
2 5
B
Start 3 Finish
2 5
B