Docsity

Graph Representation - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Graph Representation - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Graph Representation

● Adjacency list

● Adjacency matrix

● Tradeoffs:

■ What makes a graph dense? ■ What makes a graph sparse? ■ What about planar graphs?

Basic Graph Algorithms

● Breadth-first search

■ What can we use BFS to calculate? ■ A: shortest-path distance to source vertex

● Depth-first search

■ Tree edges, back edges, cross and forward edges ■ What can we use DFS for? ■ A: finding cycles, topological sort

MST Algorithms

● Prim’s algorithm

■ What is the bottleneck in Prim’s algorithm? ■ A: priority queue operations

● Kruskal’s algorithm

■ What is the bottleneck in Kruskal’s algorithm? ■ Answer: depends on disjoint-set implementation ○ As covered in class, disjoint-set union operations ○ As described in book, sorting the edges

Single-Source Shortest Path

● Optimal substructure

● Key idea: relaxation of edges

● What does the Bellman-Ford algorithm do?

■ What is the running time?

● What does Dijkstra’s algorithm do?

■ What is the running time? ■ When does Dijkstra’s algorithm not apply?

Amortized Analysis

● Idea: worst-case cost of an operation may

overestimate its cost over course of algorithm

● Goal: get a tighter amortized bound on its cost

■ Aggregate method: total cost of operation over course of algorithm divided by # operations ○ Example: disjoint-set union ■ Accounting method: “charge” a cost to each operation, accumulate unused cost in bank, never go negative ○ Example: dynamically-doubling arrays

Dynamic Programming

● Indications: optimal substructure, repeated

subproblems

● What is the difference between memoization

and dynamic programming?

● A: same basic idea, but:

■ Memoization : recursive algorithm, looking up subproblem solutions after computing once ■ Dynamic programming : build table of subproblem solutions bottom-up

Greedy Algorithms

● Indicators:

■ Optimal substructure ■ Greedy choice property : a locally optimal choice leads to a globally optimal solution

● Example problems:

■ Activity selection: Set of activities, with start and end times. Maximize compatible set of activities. ■ Fractional knapsack: sort items by $/lb, then take items in sorted order ■ MST

NP-Completeness

● What do we mean when we say a problem

is in P?

■ A: A solution can be found in polynomial time

● What do we mean when we say a problem

is in NP?

■ A: A solution can be verified in polynomial time

● What is the relation between P and NP?

■ A: P ⊆ NP , but no one knows whether P = NP

Review:

Proving Problems NP-Complete

● What was the first problem shown to be

NP-Complete?

● A: Boolean satisfiability ( SAT ), by Cook

● How do we usually prove that a problem R

is NP-Complete?

● A: Show R ∈ NP , and reduce a known

NP-Complete problem Q to R

Review:

Reductions

● Review the reductions we’ve covered:

■ Directed hamiltonian cycle  undirected hamiltonian cycle ■ Undirected hamiltonian cycle  traveling salesman problem ■ 3-CNF  k -clique ■ k -clique  vertex cover ■ Homework 7

Review: Induction

● Suppose

■ S(k) is true for fixed constant k ○ Often k = 0 ■ S(n)  S(n+1) for all n >= k

● Then S(n) is true for all n >= k

Proof By Induction

● Claim:S(n) is true for all n >= k

● Basis:

■ Show formula is true when n = k

● Inductive hypothesis:

■ Assume formula is true for an arbitrary n

● Step:

■ Show that formula is then true for n+

Induction Example:

Geometric Closed Form

● Prove a 0 + a^1 + … + a n^ = (an+1^ - 1)/(a - 1) for

all a != 1

■ Basis: show that a 0 = (a 0+1^ - 1)/(a - 1) a 0 = 1 = (a 1 - 1)/(a - 1) ■ Inductive hypothesis: ○ Assume a 0 + a 1 + … + a n^ = (a n+1^ - 1)/(a - 1) ■ Step (show true for n+1): a 0 + a 1 + … + a n+1^ = a 0 + a 1 + … + a n^ + a n+ = (a n+1^ - 1)/(a - 1) + a n+1^ = (a n+1+1^ - 1)(a - 1)

Review: Analyzing Algorithms

● We are interested in asymptotic analysis :

■ Behavior of algorithms as problem size gets large ■ Constants, low-order terms don’t matter