Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
CSE101: Design and Analysis of Algorithms
Ragesh Jaiswal, CSE, UCSD
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Course Overview
Material that will be covered in the course:
Basic graph algorithmsAlgorithm Design Techniques
Divide and ConquerGreedy AlgorithmsDynamic ProgrammingNetwork Flows
Computational intractability
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graphs
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
A way to represent a set of objects with pair-wise relationshipsamong them.
The objects are represented as vertices and the relationshipsare represented as edges.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
ExamplesSocial networksCommunication networksTransportation networksDependency networks
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Weighted graphs: There are weights associated with eachedge quantifying the relationship. For example, delay incommunication network.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Directed graphs: Asymmetric relationships between theobjects. For example, one way streets.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Path: A sequence of vertices v1, v2, ..., vk such that for anyconsecutive pair of vertices vi , vi+1, (vi , vi+1) is an edge in thegraph. It is called a path from v1 to vk .
Cycle: A cycle is a path where v1 = vk and v1, ..., vk−1 aredistinct vertices.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Strongly connected: A graph is called strongly connected ifffor any pair of vertices u, v , there is a path from u to v and apath from v to u.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Tree: A strongly connected, undirected graph is called a treeif it has no cycles.
How many edges does a tree have?
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Let P(n) be the statement
Any tree with n nodes has exactly n − 1 edges.
An inductive proof will have the following steps:
Base case: Show that P(1) is true.Inductive step: Show that if P(1),P(2)...,P(k) are true, then so isP(k + 1).
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Let P(n) be the statement
Any tree with n nodes has exactly n − 1 edges.
An inductive proof will have the following steps:
Base case: Show that P(1) is true.Inductive step: Show that if P(1),P(2)...,P(k) are true, then so isP(k + 1).
Proof outline
Base case: P(1) is true since any tree with 1 vertex has 0 edges.Inductive step: Assume that P(1), ...,P(k) are true.
Now, consider any tree T with k + 1 vertices.Claim 1: There is a vertex v in T that has exactly 1 edge.Consider the tree T ′ obtained by removing v and its edge from T .Claim 2: T ′ is a tree with k vertices.As per the induction hypothesis, T ′ has k − 1 edges. This impliesthat T has k edges.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsIntroduction
Proof
Base case: P(1) is true since any tree with 1 vertex has 0 edges.Inductive step: Assume that P(1), ...,P(k) are true.
Now, consider any tree T with k + 1 vertices.Claim 1: There is a vertex v in T that has exactly 1 edge.
Proof: For the sake of contradiction, assume that there does notexist such a vertex in T . Then this means that all vertices have atleast two edges incident on them. Start with an arbitrary vertex u1in T . Starting from u1 use one of the edges incident on u1 to visitits neighbor u2. Since u2 also has at least two incident edges, takeone of the other edges to visit its neighbor u3. On repeating this,we will (in finite number of steps) visit a vertex that was alreadyvisited. This implies that there is a cycle in T . This is acontradiction.
Consider the tree T ′ obtained by removing v and its edge from T .Claim 2: T ′ is a tree with k vertices.
Proof: T ′ clearly has k vertices. T ′ is strongly connected sinceotherwise T is not strongly connected. Also, T ′ does not have acycle since otherwise T has a cycle.
As per the induction hypothesis, T ′ has k − 1 edges. This impliesthat T has k edges.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsData Structures
Adjacency matrix: Store connectivity in a matrix.
Space: O(n2)
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
GraphsData Structures
Adjacency list: For each vertex, store its neighbors.
Space: O(n + m)
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph Algorithms
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsGraph exploration
Problem
Given an (undirected) graph G = (V ,E ) and two vertices s, t,check if there is a path between s and t.
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsGraph exploration
Problem
Given an (undirected) graph G = (V ,E ) and two vertices s, t,check if there is a path between s and t.
Alternate problem: What are the vertices that are reachablefrom s. Is t among these reachable vertices?
This is also known as graph exploration. That is, explore allvertices reachable from a starting vertex s.
Breadth First Search (BFS)Depth First Search (DFS)
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
Theorem 1: The shortest path from s to any vertex in Layer(i) isequal to i .
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Theorem 1: The shortest path from s to any vertex in Layer(i) isequal to i .
Proof sketch
We will prove by induction. Let P(i) denote the statement:The shortest path from s to any vertex in Layer(i) is equal to i .We will prove that P(i) is true for all i using induction.Base case: P(0) is true since Layer(0) contains s.Inductive step: Assume P(0), ...,P(k) are true. We will show thatP(k + 1) is true.
Assume for the sake of contradiction that P(k + 1) is not true.This implies that there is a vertex v in Layer(k + 1) such that theshortest path length from s to v is < k + 1 (the case > k + 1 isskipped for class discussion)Consider such a path P from s to v .Claim 1: There exists a vertex u in the path P that is contained inLayer(k).This gives us a contradiction since by induction hypothesis, theshortest path length from s to u is k .
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
What is the running time of BFS given that the graph is given inadjacency list representation?
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
Graph AlgorithmsBFS
Breadth First Search (BFS)
BFS(G , s)- Layer(0) = {s}- i ← 1- While(true)
- Visit all new nodes that have an edge to a vertex in Layer(i − 1)- Put these nodes in the set Layer(i)- If Layer(i) is empty, then end- i ← i + 1
What is the running time of BFS given that the graph is given inadjacency list representation? O(n + m)
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms
End
Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms