# Lecture 025

## Definition of Tree and Forest

### Definition of Cycles

Cycle: a path from a vertex to itself

• 0-1-4-0

• 0-1-0

• 0

Simple Cycle: with at least one edge and without repeated edges

• 0-1-4-0 only

### Definition of Trees

Tree: a connected graph without simple cycles

Definitions:

• a connected graph with no simple cycles

• a vertex, or two trees connected by an edge (rec)

• a vertex, or a tree connected to a vertex by an edge (rec)

• a connected graph with v vertices and v-1 edges

• a connected graph with exactly 1 path between any two vertices

### Definition of Forests

Forest: a graph where each connected component is a tree (notice a tree is a forest)

• a graph with (connected or not) no simple cycles

• a graph with at most one path between any two vertices

• a forest with v vertices has at most v-1 edges

### Complexity

DFS, BSF complexity of a tree: O(v) reduced from O(v+e) unlike graph

## Spanning Tree

### Binary Search Tree

BST: a tree where every vertex has at most 3 edges

• two children, one parent

• ordering invariant

• root = any vertex with at most 2 edges

### Spanning Tree

• Space: O(v)

• providing a path from every vertex to every other vertex

subgraph: a subgraph of graph G is a graph with the same vertices and a subset of its edges

spannning tree: a spannning tree for G is a subgraph that

• has the same connectivity as G

• and is a tree

spanning forest: a spanning forest for G is a subgraph that (a bunch of spanning trees)

• has the same connectivity as G

• and is a forest

### Computing a Spanning Tree

#### Edge-Centric algorithm (O(ev)?O(elog(v)))

Start with a spanning forest of singleton trees and add edges from the graph as long as they don't form a cycles (def.B)

Initialize T with isolated vertices of G (O(1)=graph_new)
For each edge (u, v) in G: (O(e))
if connected(u, v): (O(v) where v=v_in_tree_so_far by DFS/BFS)
else:
stop once T has v-1 edges

costs O(ev)

- O(v^2) for sparse graphs


(it can create spanning forests) (Edge-Centric algorithm is greedy, DFS/BFS is not since they have worklist)

##### Making a Maze

• first connect all notes

• build a spanning tree

• pick a start and an end

• solving a maze is BFS/DFS

#### Vertex-Centric algorithm (O(e))

Start with a single vertex in the tree and add edges to vertices not in the tree (def.C)

(Complexity: O(e) as DFS/BFS)

## Minimum Spanning Tree

Minimum spanning tree: one with the least total weight of its edges.

• a graph may have several minimum spanning trees

### Kruskal's Algorithm (O(ev)->O(elog(e)))

• total is O(ev)

• sort is O(e log(e))

• O(elog(e) + ev) above
• since log(e) \in O(v)
• because e \in O(v^2), so log e \in O(log v)
• and log v \in O(v)

#### Union Find: A way to check connectivity by grouping O(ev)

O(ev)

• check if two point to the same representative (O(v))

• merge representative, always have 2 choice (don't merge node) O(1)

• stop once T has v-1 edges

#### Height Tracking: Trying to build balanced tree by tracking height (O(elog(e)))

• store length in root as negative

• merge shorter trees into taller trees

• initialize array into "-1"s

• A tree T of height h has at least $2^{h-1}$ vertices

• A tree T with v vertices has height at most $log(v)+1$ (balanced tree = O(log v), for loop O(e), total O(eloge + elog v), assume sparse O(eloge))

#### Path Compression (O(eAck^{-1}(v)) amortized)

• grows very fast, inverse grows slow

### Prim's Algorithm (O(e log e))

• adding/removing edges is O(log e) in priority queue

• complexity O(e log e)

Table of Content