Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Some

Terminology
•  Tree
–  Connected, acyclic graph

1
Some Terminology
•  Tree
–  Connected, acyclic graph
•  Spanning tree
–  Subgraph which is a tree, and connects every
vertex of original graph

2
Some Terminology
•  Tree
–  Connected, acyclic graph
•  Spanning tree
–  Subgraph which is a tree, and connects every
vertex of original graph
•  Minimum spanning tree
–  Spanning tree with least total weight among all
other spanning trees

3
Applications?

4
Applications?
•  Situation: Cable company wants to lay copper
wire/optical fiber in a new neighborhood
–  Some paths more expensive than others

5
Applications?
•  Situation: Cable company wants to lay copper
wire/optical fiber in a new neighborhood
–  Some paths more expensive than others
•  MST provides cheapest layout that achieves
total connectivity to every home

6
Applications?
•  Situation: Cable company wants to lay copper
wire/optical fiber in a new neighborhood
–  Some paths more expensive than others
•  MST provides cheapest layout that achieves
total connectivity to every home
•  Other examples:
–  Road layout
–  Cell tower placement
–  Wiring in house
7
Spanning Tree Problem
•  Graph G=(V, E), is connected
–  Consider only undirected graphs
•  Find T as a subset of E where:
–  (V, T) is connected
–  (V, T) has no cycles

8
Spanning Tree Properties
•  Connected graph G can have more than one
spanning tree
•  All possible spanning trees of G have the same
number of edges and vertices
•  Spanning trees do not have cycles
•  Removing one edge from a spanning tree
results in disconnected tree
•  Adding one edge to a spanning tree creates a
cycle
9
Spanning Tree Properties
•  At most VV-2 spanning trees for any “fully
connected” graph (each vertex has a direct
edge to every other vertex)

10
Minimum Spanning Tree Problem
•  For a weighted graph, MST is the ST with the
least total weight among all other STs
●  Input: Undirected Graph G = (V, E) and a cost
function C. C(e) is the cost of edge e.
●  Output: A spanning tree T with minimum
total cost

11
Two Different Solutions

12
Prim-Jarnik Algorithm
1.  Pick a random vertex as the “root”, and place all
vertices into “unknown” set
2.  Set unknown vertex weights to infinity/max, set root
vertex weight to 0
3.  Find and remove vertex with least weight from
unknown set, place it into “known” set, this removed
vertex is the “current” vertex
4.  Update the weights of each unknown vertex adjacent
to current vertex (by comparing current weight to
weight of edges, smaller weight always wins), update
parent for each weight update that occurred
5.  Repeat 3-5 until all vertices are known (in the
“known” set, and the unknown set is empty)

13
Example
•  Start at S (as root)
•  Can start anywhere, why?

14
Example
•  Check adjacent edges, select least weight

15
Example

16
Example

17
Example

18
Example 2

19
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances

20
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances
●  Depends on data structure used!

21
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances
●  Depends on data structure used!

○  Adjacency matrix?

22
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances
●  Depends on data structure used!

○  Adjacency matrix? O(V2)

23
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances
●  Depends on data structure used!

○  Adjacency matrix? O(V2)


○  Adjacency list with min-heap?

24
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances
●  Depends on data structure used!

○  Adjacency matrix? O(V2)


○  Adjacency list with min-heap? O(VlogV + ElogV)

25
Time Complexity?
Set up all infinite distances, and root/source as 0
While unknown !empty
Find min distance vertex from unknown
For each adjacent vertex
Update distances
●  Depends on data structure used!

○  Adjacency matrix? O(V2)


○  Adjacency list with min-heap? O(VlogV + ElogV)
○  Adjacency list with Fibonacci heap: O(E + VlogV)
26
Find the MST . . .

27
Homework 3
•  You have started the Bus-ber company. Your
service is like Uber, but each driver operates a
bus. The bus holds a certain number of people.
Find a route* that is efficient for dropping
everyone off.
•  Find the MST of the graph.
–  *MST does not find a “route”, but is close to an actual
route.
•  Some key data structures:
–  person (vertex), route (edge)
–  Graph is fully connected
–  HashMap
•  HashMap< person, HashMap<person, route> >
–  Set 28
Homework 3
•  HashMap (“matrix” variable)
–  Hash table which stores “key” with a
corresponding “value”
–  Typically, “key” and “value” are primitive data
types
–  Bus-ber has non-primitive data types (HashMap of
HashMaps)
•  Key = person
•  Value = HashMap< person, route >

29
Homework 3
•  Set
–  A mathematical set (unique keys)
–  Used for keeping track of unknown/unvisited
vertices
–  Need to copy the existing matrix and produce a
keySet from the copied matrix
•  Otherwise original matrix will be modified
•  Removing from a keySet will also remove from the
matrix

30

You might also like