# Lecture 3

## Greedy Algorithms

• Constructing a solution step-by-step
• Once you make a decision (make a step), there is no going back
• Each step should be the local optimum, hoping it works out for a global optimum
• It can also be used to find the least optimal step in a solution, since we can compare a solution to our greedy algorithm’s solution

### Minimum spanning tree (MST)

Given a connected, undirected graph, the minimum spanning tree is a fully connected tree where there are the minimum number of edges (weights should be considered too).

#### Applications of MST

• Network designs: understanding the most optimum way to connects hosts in a local area network
• Approximations of NP-Hard Problems: Travelling salesman etc.

### General properties of MSTs

Cut Property: We have a set of $S$ vertices. We look at an edge $e$ as the minimum cost edge from $S$ to another set of vertices.

Cycle Property: If we have a cycle, then there is always an edge with the longest weight, which we can remove.

Cutset Intersection: The edges in the cycle that are also in the cutset.

### Prim’s Algorithm (Dijkstra)

Initialise $S =$ any node. Apply the cut property to $S$. Add a minimum cost edge to the cutset and add one new explored node to $S$. Every time there’s a cutset intersection, we remove the most expensive edge

#### Analysis of Prim’s Algorithm

Runtime is $O(m \log n)$ with a binary heap. It is more likely $O(n^2)$ if using the basic array structure

### Kruskal’s Algorithm

Consider edges in ascending order of weight

Case 1: If adding $e$ to $T$ creates a cycle, discard $e$.

Case 2: Otherwise, insert $e = (u,v)$ into $T$ according to the cut property.

Importantly, Kruskal’s algorithm will create disconnected components. Once there are no more available edges that don’t create a cycle, we are done

##### Algorithm

In this case, we use an efficient merge algorithm to merge sort the set.