29
CSE101: Design and Analysis of Algorithms Ragesh Jaiswal, CSE, UCSD Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

CSE101: Design and Analysis of Algorithms

Ragesh Jaiswal, CSE, UCSD

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 2: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 3: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

Graphs

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 4: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 5: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

GraphsIntroduction

ExamplesSocial networksCommunication networksTransportation networksDependency networks

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 6: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 7: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

GraphsIntroduction

Directed graphs: Asymmetric relationships between theobjects. For example, one way streets.

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 8: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 9: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 10: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 11: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 12: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 13: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 14: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

GraphsData Structures

Adjacency matrix: Store connectivity in a matrix.

Space: O(n2)

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 15: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

GraphsData Structures

Adjacency list: For each vertex, store its neighbors.

Space: O(n + m)

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 16: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

Graph Algorithms

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms

Page 17: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 18: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 19: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 20: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 21: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 22: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 23: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 24: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 25: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 26: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 27: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 28: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

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

Page 29: CSE101: Design and Analysis of Algorithmscseweb.ucsd.edu/~rajaiswal/Winter2020/cse101/Slides/Week-01/lec … · 2;:::;v k such that for any consecutive pair of vertices v i;v i+1,

End

Ragesh Jaiswal, CSE, UCSD CSE101: Design and Analysis of Algorithms