4 - Intro to Graphs and Greedy Algos
ucla | CS 180 | 2023-10-10 08:14
Table of Contents
Priority Queue
- in-class priority queue implementation uses a heap
MinHeap
- the root of all subtrees is smaller than the children (less priority)
- no guarantees about right vs left children
- can access a node and its children in
- can be in an array or linked list format
Array format
- node at
has children at and - children have parent at
and
Heapify
- when we access the min, we remove it
- then set the heap root to
- on a balanced tree heapify ->
- rebalancing may cause non-heap leaves -> do a minimum of this lea and the parent and place that as the sub root
- heap sort (sorting using a heap)
- insertion and deletions
- heapify sorting down is
Graphs
Structure
- made of vertices/nodes
- nodes connected by edges/links
the graph is defined as the set of nodes and edges:$$G=(V,E)\quad\text{s.t.}\quadV =n,\space E =m$$ - edges can be directed or undirected
Properties
- nodes are adjacent if an edge connects them (in a directed adjacency only follows the direction)
- bidirectionality requires 2 edges
- graphs are connected if all vertices are connected by edges
- disconnected subgraphs are called components
- path is deined by set of eges e.g.,
- cycle is a path that begins and ends at the same point
- simple paths/cycles visit vertices only once
Exploration
- many ways to store; one way is an adjacency matrix
- number of edges
- a graph has at least
edges and ranging from sparse to dense edges s.t.Greedy Algorithms
- says to pick the locally optimal choice at every step (don’t consider global maximization)
- usually based on a parameter of the inputs: length of tasks, number of conflicts, etc.
Scheduling problem
- tasks are given as an interval
- tasks can only be completed in sequence (no parallelization)
- pick the maximum subset of non-overlapping tasks
Example
Greedy Solution
- pick tasks based on the earliest end time that does not overlap
Proof BWOC
Implementation - no overlaps
- sort the list of events by earliest finish date (e.g. using heapsort)
- pick the first event
- then, pick in order IF the event’s start time is after the already picked start time
- thus, the total run time: