MAT214ALecture4 19

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

MAT214A

Friday 4/19
Spring 2019 10:50am - 12:05pm

 Homework for Section 9.3: 2, 3, 5, 6 and 9.4: 2, 3, 10, 11, 14, 15


 Take home 9.1-9.3 Quiz due
 Final Exam –Friday 5/3 at 11 am – 1 pm
Calculator, 3 inch by 5 inch note card both sides

Recall:
Section 9-3 Spanning Trees
Sometimes, you are given graph, but only need one way to connect nodes. Think of communication networks,
transportation networks, electrical grids, etc.
 A spanning tree T of a graph G is a subgraph of G that connects all the vertices of G.
 A spanning tree is not unique unless the graph is a tree graph

Methods to find spanning trees:


#1 Breadth First Search (BFS)
#2 Depth First Search (DFS)

Breadth First Search (BFS)


Process all vertices on a given level before moving to the next level.
At each level add to the tree all edges and vertices incident to those edges – that do not create a cycle.
Repeat until all vertices are included in tree

The breadth first search tree is a shortest path tree starting from its root. *BFS tree is short and “branchy”

Depth First Search (DFS)


We start the graph traversal at an arbitrary vertex and go down a particular branch until we reach a dead end.
If all vertices are included, we are done. Otherwise, we move back to most recent vertex in the path that
allows a new path through unvisited vertices. Repeat until all vertices are visited.

The idea of this algorithm is to make a path as long as possible, and then go back (backtrack) to add branches
also as long as possible. *The DFS tree is typically "long and stringy"
Section 9-4 Minimal Spanning Trees
Given a weighted graph, find a minimum weight subgraph that contains all vertices.
 Must be a spanning tree – must contain all vertices
 Must be connected – so every vertex can be reached from every other
 Must have one simple path between each pair of vertices – multiple paths would not produce a minimum
weight.

How to find a minimal weight spanning tree: Prim’s Algorithm


Begin with a single vertex
Add minimum weight edge that does not contain a cycle
Repeat for ALL terminating vertices on tree.

Ex: Graph 9.4.1

Step 1:
Assume start is vertex 1
Edge Weight
(1,5) 3
(1,3) 2 select
(1,2) 4

Step 2:
V = {1, 3}, E = {(1,3)}

Edge Weight
(1,5) 3
(1,2) 4
(3,4) 1 select
(3,6) 3
(3,5) 6

Step 3:
V = {1, 3, 4}, E = {(1,3), (3,4)}
Edge Weight
(1,5) 3 select
(1,2) 4
(3,6) 3
(3,5) 6
(4,6) 3
(4,2) 5
**I could select either e(1,5) or e(4,6). We will choose e(1,5)
Step 4:
V = {1, 3, 4, 5}, E = {(1,3), (3,4) (1,5)}
Edge Weight
(1,2) 4
(3,6) 3
(3,5) 6
(4,6) 3
(4,2) 5
(5,6) 2 select

Step 5:
V = {1, 3, 4, 5, 6}, E = { (1,3), (3,4), (1,5), (5,6)}
Edge Weight
(1,2) 4 select
(3,6) 3
(4,6) 3
(4,2) 5

End: all vertices visited


V = {1, 3, 4, 5, 6, 2}, E = { (1,3), (3,4), (1,5), (5,6), (1,2)}
Minimal spanning tree – weight is 4 + 2 +1 + 3 + 2 = 12

1 2

3 4

5 6

 Prim’s Algorithm is a “greedy algorithm” in that it optimizes choice at each iteration.

 Note that this is not the same as the “nearest neighbor” approximation approach to the Traveling
Salesman Problem. The “nearest neighbor” selects nearest unvisited vertex incident to most recently
added vertex. This does not always give the minimum weight tree.

Example here – not optimal

Homework #1

Section 9-5 Binary Trees


 The structure of binary trees maps naturally to a sequence of if…then…else decisions.
A tree is a binary tree if every vertex has degree at most 3.
Every vertex has at most 2 children, hence the name binary.

Def: A binary tree is a rooted tree in which each vertex has at most 2 children.
If a vertex has 2 children, we label right or left. If a vertex has only one child, we name as either right or left.
Binary tree:

Def: A full binary tree is a binary tree where each vertex has 0 or 2 children.
Full binary tree:

Theorem: If T is a full binary tree with I internal vertices, then T has i + 1 terminating vertices and 2i + 1 total
vertices.
Proof: Each internal vertex has 2 children = 2i
Root is a “nonchild” =1
Total vertices =2i + 1

Proof: The number of terminal vertices = (total vertices) – (internal vertices)


= 2i + 1 - i
=i+1
Done

Example: Show that if n teams play in a single elimination tournament, n-1 matches are played.
This is a full binary tree where each node has 0 or 2 children.

Teams are the terminal vertices. There are n teams so n terminating vertices
We know from theorem that there are i + 1 terminating vertices, where i = # of internal vertices
So, n = i+ 1
We know that the matches are the internal vertices – there are two teams playing, winner is vertex. The
number of matches = i
Substituting: n = #matches + 1, rearranging #matches = n – 1
Done

Example: Once the teams are assigned, how many ways can the tournament be played?
There are n – 1matches, each match has two possible outcomes.
There are 2n-1 possible ways the tournament can be played

Theorem: If a binary tree of height h has t terminal vertices, then t ≤ 2 h


Recall that the height h = # of levels

Height = 4 levels There are t ≤ 24 terminal vertices Height = 3 levels There are t ≤ 23 terminal vertices
t ≤16 terminal vertices t ≤ 8 terminal vertices
5 ≤ 16 5≤8

Stopped here ----------------------------------

You might also like