Design and Analysis of Algorithms

Similar documents
Chapter 14 Section 3 - Slide 1

EECS 203 Lecture 20. More Graphs

Discrete mathematics

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Math 776 Graph Theory Lecture Note 1 Basic concepts

Varying Applications (examples)

Elements of Graph Theory

Institute of Operating Systems and Computer Networks Algorithms Group. Network Algorithms. Tutorial 1: Flashback and Fast Forward

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

The Konigsberg Bridge Problem

Euler and Hamilton paths. Jorge A. Cobb The University of Texas at Dallas

Graph Theory S 1 I 2 I 1 S 2 I 1 I 2

Chapter 5: Euler Paths and Circuits The Mathematics of Getting Around

14 More Graphs: Euler Tours and Hamilton Cycles

Chapter 5: Euler Paths and Circuits The Mathematics of Getting Around

Introduction to Graph Theory

How can we lay cable at minimum cost to make every telephone reachable from every other? What is the fastest route between two given cities?

Brief History. Graph Theory. What is a graph? Types of graphs Directed graph: a graph that has edges with specific directions

1. The Highway Inspector s Problem

Math 778S Spectral Graph Theory Handout #2: Basic graph theory

Some Graph Theory for Network Analysis. CS 249B: Science of Networks Week 01: Thursday, 01/31/08 Daniel Bilar Wellesley College Spring 2008

COMP108 Algorithmic Foundations

An Introduction to Graph Theory

Graphs (MTAT , 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao,David Tse Note 8

More NP-complete Problems. CS255 Chris Pollett May 3, 2006.

Algorithms and Data Structures

Notebook Assignments

Number Theory and Graph Theory

V1.0: Seth Gilbert, V1.1: Steven Halim August 30, Abstract. d(e), and we assume that the distance function is non-negative (i.e., d(x, y) 0).

TOTAL CREDIT UNITS L T P/ S SW/F W. Course Title: Analysis & Design of Algorithm. Course Level: UG Course Code: CSE303 Credit Units: 5

Combinatorial Optimization

6.2. Paths and Cycles

Introduction III. Graphs. Motivations I. Introduction IV

COMP108 Algorithmic Foundations

1 Variations of the Traveling Salesman Problem

1 The Traveling Salesperson Problem (TSP)

Assignments are handed in on Tuesdays in even weeks. Deadlines are:

Ma/CS 6a Class 8: Eulerian Cycles

Fundamental Properties of Graphs

Chapter 11: Graphs and Trees. March 23, 2008

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

Lecture 1: Examples, connectedness, paths and cycles

Chapter 3: Paths and Cycles

Algorithms and Data Structures, or

Week 10: Colouring graphs, and Euler s paths. 14 and 16 November, 2018

Design and Analysis of Algorithms. Comp 271. Mordecai Golin. Department of Computer Science, HKUST

Lecture 22 Tuesday, April 10

Graphs and trees come up everywhere. We can view the internet as a graph (in many ways) Web search views web pages as a graph

Graph Algorithms. Tours in Graphs. Graph Algorithms

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Graph Theory. 1 Introduction to Graphs. Martin Stynes Department of Mathematics, UCC January 26, 2011

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 7

Instant Insanity Instructor s Guide Make-it and Take-it Kit for AMTNYS 2006

CSE 417 Practical Algorithms. (a.k.a. Algorithms & Computational Complexity)

Launch problem: Lining streets

Logic: The Big Picture. Axiomatizing Arithmetic. Tautologies and Valid Arguments. Graphs and Trees

INTRODUCTION TO GRAPH THEORY. 1. Definitions

Lecture 5: Graphs. Rajat Mittal. IIT Kanpur

Notes for Lecture 24

8 NP-complete problem Hard problems: demo

WUCT121. Discrete Mathematics. Graphs

Introduction to Engineering Systems, ESD.00. Networks. Lecturers: Professor Joseph Sussman Dr. Afreen Siddiqi TA: Regina Clewlow

Lecture 8: The Traveling Salesman Problem

Discrete mathematics II. - Graphs

Packet #6: Counting & Graph Theory. Applied Discrete Mathematics

Network Topology and Graph

Algorithms and Theory of Computation. Lecture 1: Introduction, Basics of Algorithms

Graph Theory: Starting Out

Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures. Algorithms and Data Structures

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. An Introduction to Graph Theory

Graphs: Definitions Trails, Paths, and Circuits Matrix Representations Isomorphisms. 11. Graphs and Trees 1. Aaron Tan. 30 October 3 November 2017

CHAPTER 10 GRAPHS AND TREES. Copyright Cengage Learning. All rights reserved.

SEVENTH EDITION and EXPANDED SEVENTH EDITION

Outline. Graphs. Divide and Conquer.

Introduction to Approximation Algorithms

Chapter 8 Topics in Graph Theory

Discrete mathematics

CS 217 Algorithms and Complexity Homework Assignment 2

Graphs and Puzzles. Eulerian and Hamiltonian Tours.

Basics of Graph Theory

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Partha Sarathi Mandal

Graph Theory CS/Math231 Discrete Mathematics Spring2015

Graph Data Processing with MapReduce

Approximation Algorithms

Introduction to Graphs

Stanford University CS261: Optimization Handout 1 Luca Trevisan January 4, 2011

Sarah Will Math 490 December 2, 2009

CHAPTER 10 GRAPHS AND TREES. Alessandro Artale UniBZ - artale/z

CSE 548: Analysis of Algorithms. Lecture 13 ( Approximation Algorithms )

Graphs and Isomorphisms

Graph Theory Part A. Peter Keevash

Chapter 2 Graphs. 2.1 Definition of Graphs

CS 311 Discrete Math for Computer Science Dr. William C. Bulko. Graphs

Virtual University of Pakistan

Basic Combinatorics. Math 40210, Section 01 Fall Homework 4 Solutions

Graphs and Algorithms 2015

1 Introduction. 1. Prove the problem lies in the class NP. 2. Find an NP-complete problem that reduces to it.

Transcription:

CS4335: Design and Analysis of Algorithms Who we are: Dr. Lusheng WANG Dept. of Computer Science office: B6422 phone: 2788 9820 e-mail: lwang@cs.cityu.edu.hk Course web site: http://www.cs.cityu.edu.hk/~lwang/ccs3335.html Wong Tsui Fong Helena will do tutorials for all groups. Lecture: Tuesday (15:30-17:20) Monday (19:30-21:20) Page 1

Text Book: References: J. Kleinberg and E. Tardos, Algorithm design, Addison-Wesley, 2005. We will add more material in the handout. T. H. Cormen, C. E. Leiserson, R. L. Rivest, Introduction to Algorithms, The MIT Press. http://theory.lcs.mit.edu/~clr/ R. Sedgewick, Algorithms in C++, Addison-Wesley, 2002. A. Levitin, Introduction to the design and analysis of algorithms, Addison-Wesley, 2003. M.R. Garry and D. S. Johnson, Computers and intractability, a guide to the theory of NP-completeness, W.H. Freeman and company, 1979 Page 2

Page 3

Algorithms Any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. A sequence of computational steps that transform the input into output. A sequence of computational steps for solving a well-specified computational problem. Page 4

Example of well-specified problem: Sorting Input: a sequence of numbers: 1, 100, 8, 25, 11, 9, 2, 1, 200. Output: a sorted (increasing order or decreasing order) sequence of numbers 1, 2, 8, 9, 11, 25, 100, 200. Another example: Create web page that contains a list of papers using HTML. -everybody can do it. Not need to design computational steps. Page 5

B A Find a shortest path from station A to station B. -need serious thinking to get a correct algorithm. Page 6

A Real-Time Driver s Direction System Given an electronic map (stored on a computer), the position of your car (provided by GPS), and the destination, the system can tell you the way to go to the destination. Tell you tern left or right 40 meters before according to the shortest path. If you did not follow the direction, re-calculate the shortest path. 400 US$ each. Page 7

On-line Auction: maximize the benefit There are a set of customers and a set of products. Customer Products 2 2 3 2 Every customer can buy only one. Decide: which product goes to which customer. Maximum Matching for Bipartite Graph Page 8

Descriptions of Algorithms Flow chart Pseudo-code Programs Natural languages The purpose: Allow a well-trained programmer to write a program to solve the computational problem. Any body who can talk about algorithm MUST have basic programming skills Also CS3334: Data structures. Page 9

What We Cover: 1. Some classic algorithms in various domains Graph algorithms Euler path, shortest path, minimum spanning trees, maximum flow, Hamilton Cycle, traveling salesman, String Algorithms Exact string matching, Approximate string matching, Applications in web searching engines Scheduling problems Computational Geometry 2. Techniques for designing efficient algorithms divide-and-conquer approach, greedy approach, dynamic programming Page 10

What We Cover(continued): 3. Introduction to computational complexity NP-complete problems 4. Approximation algorithms Vertex cover Steiner trees Traveling sales man problem Page 11

Why You have to take this course You can apply learned techniques to solve various problems Have a sense of complexities of various problems in different domains College graduates vs. University graduates Supervisor v.s. low level working force Page 12

A joke: The boss wants to produce programs to solve the following two problems Euler circuit problem: given a graph G, find a way to go through each edge exactly once. Hamilton circuit problem: given a graph G, find a way to go through each vertex exactly once. The two problems seem to be very similar. Person A takes the first problem and person B takes the second. Outcome: Person A quickly completes the program, whereas person B works 24 hours per day and is fired after a few months. Page 13

Euler Circuit: The original Konigsberg bridge Page 14

Hamilton Circuit Page 15

A joke (continued): Why? no body in the company has taken CS3335. Explanation: Euler circuit problem can be easily solved in polynomial time. Hamilton circuit problem is proved to be NP-hard. So far, no body in the world can give a polynomial time algorithm for a NP-hard problem. Conjecture: there does not exist polynomial time algorithm for this problem. Page 16

Page 17

Page 18

Page 19

Evaluation of the Course Course work: 30% Four assignments 6 points for each of the first three assignments 12 points for the last assignment Working load is Not heavy A final exam: 70%; Page 20

How to Teach Contents are divided into four classes 1. Basic part -- every body must understand in order to pass 2. Moderate part -- most of students should understand. Aim at B or above. 3. Hard part -- used to distinguish students. Aim at A or above. 4. Fun part -- just illustrate that some interesting things can be done if one works very hard. -- useful knowledge that will not be tested. -- I will give some challenging problems for fun. Page 21

Challenging Problems For those who have extra energy, we have challenging problems. Challenging problems will be distributed at the end of some lectures. No mark will be given for those challenge problems. I will record who completely solved the problem. (The records will be used to decide the boundary cases.) Good Training for solving new problems Have high chance to solve the hard problems in the final exam. Helpful for getting good marks in the final exam. Page 22

How to Learn 1. Attend every lecture (2 hours per week) and tutorial (1 hour per week) 2. Try to go with me when I am talking 3. Ask questions immediately 4. Try to fix all problems during the 1 hour tutorial 5. Ask others. Page 23

The Process of Design an Algorithm Formulating the problem with enough mathematical precision we can ask a concrete question start to solve it. Design the algorithm list the precise steps. (an expert can translate the algorithm into a computer program.) Analyze the algorithm prove that it is correct establish the efficiency the running time or sometimes space Page 24

Terminologies A Graph G=(V,E): V---set of vertices and E--set of edges. Path in G: sequence v 1, v 2,..., v k of vertices in V such that (v i, v i+1 ) is in E. v i and v j could be the same Circuit: A path v 1, v 2,..., v k such that v 1 = v k. Degree of a vertex: number of edges incident to the vertex. Page 25

Euler circuit Input: a connected graph G=(V, E) Problem: is there a circuit in G that uses each edge exactly once. Note: G can have multiple edges,.i.e., two or more edges connect vertices u and v. Page 26

Story: The problem is called Konigsberg bridge problem it asks if it is possible to take a walk in the town shown in Figure 1 (a) crossing each bridge exactly once and returning home. solved by Leonhard Euler [pronounced OIL-er] (1736) The first problem solved by using graph theory A graph is constructed to describe the town. (See Figure 1 (b).) Page 27

The original Konigsberg bridge (Figure 1) Page 28

Theorem for Euler circuit Theorem 1 (Euler s Theorem) A connected graph has an Euler circuit if and only if all the vertices in the graph have even degree. Proof: if an Euler circuit exists, then the degree of each node is even. Going through the circuit, each time a vertex is visited, the degree is increased by 2. Thus, the degree of each vertex is even. Page 29

Proof of Theorem 1: if the degree of every node is even, then there is an Euler circuit. We give way to find an Euler circuit for a graph in which every vertex has an even degree. Since each node v has even degree, when we first enter v, there is an unused edge that can be used to get out v. The only exception is when v is a starting node. Then we get a circuit (may not contain all edges in G) If every node in the circuit has no unused edge, all the edges in G have been used since G is connected. Otherwise, we can construct another circuit, merge the two circuits and get a larger circuit. In this way, every edge in G can be used. Page 30

Example 1: C1:abca, C2:bdefb, =>C3:abdefbca. C4:eijhcge. C3+C4=>abdeijhcgefbca a 13 7 c h 1 2 d 3 b 11 10 e f 12 9 8 g i 6 5 j 4 a 1 b 3 c 2 a 1 b 7 c 6 4 d 5 b 7 6 e f after merge 2 d 3 5 4 e f c 13 12 g 8 11 h 10 9 e i j Page 31

An efficient algorithm for Euler circuit 1. Starting with any vertex u in G, take an unused edge (u,v) (if there is any) incident to u 2. Do Step 1 for v and continue the process until v has no unused edge. (a circuit C is obtained) 3. If every node in C has no unused edge, stop. 4. Otherwise, select a vertex, say, u in C, with some unused edge incident to u and do Steps 1 and 2 until another circuit is obtained. 5. Merge the two circuits obtained to form one circuit 6. Goto Step 3. Page 32

Example 2: e a c f b d First circuit: a-b-d-c-a Second circuit: d-f-e-c-d. Merge: a-b-d-f-e-c-d-c-a. 3 rd circuit: e-g-h-e Merge: a-b-d-f-e-g-h-e-c-d-c-a Note: There are multiple edges. g h Page 33

Representations of Graphs Two standard ways Adjacency-list representation Space required O( E ) Adjacency-matrix representation Space required O(n 2 ). Depending on problems, both representations are useful. Page 34

Adjacency-list representation Let G=(V, E) be a graph. V set of nodes (vertices) E set of edges. For each u V, the adjacency list Adj[u] contains all nodes in V that are adjacent to u. 1 2 5 / 1 2 2 1 5 3 4 / 3 3 2 4 / 5 4 4 5 2 4 5 1 3 / 2 / (a) (b) Page 35

Adjacency-list representation for directed graph 1 2 4 / 1 2 3 2 5 / 3 6 5 / 4 5 6 4 5 2 / 4 / 6 6 / (a) (b) Page 36

Adjacency-matrix representation Assume that the nodes are numbered 1, 2,, n. The adjacency-matrix consists of a V V matrix A=(a ij ) such that a ij = 1 if (i,j) E, otherwise a ij = 0. 1 2 3 4 5 1 2 1 0 1 0 0 1 3 2 1 0 1 1 1 5 4 3 0 1 0 1 0 4 0 1 1 0 1 (a) 5 1 1 0 1 0 (c) Page 37

Adjacency-matrix representation for directed graph It is NOT symmetric. 1 2 3 4 5 6 1 2 3 1 0 1 0 1 0 0 2 0 0 0 0 1 0 4 5 6 3 0 0 0 0 1 1 4 0 1 0 0 0 0 (a) 5 0 0 0 1 0 0 6 0 0 0 0 0 1 (c) Page 38

Implementation of Euler circuit algorithm (Not required) Data structures: Adjacency-list representation Each node in V has an adjacency list Also, we have two lists to store the circuits One for the circuit produced in Steps 1-2. One for the circuit produced in Step 4 In Step 1: when we take an unused edge (u, v), this edge is deleted from the adjacency-list of node u. Page 39

Implementation of Euler circuit algorithm In Step 2: if the adjacency list of v is empty, v has no unused edge. 1. Testing whether adjacency-list v is empty. 2. A circuit (may not contain all edges) is obtained if the above condition is true. 3. If the adjacency-list for v is empty, DELETE the list. In Step 3: if all the adjacency-list is empty, stop. (Note we did 3). In step 4: if some adjacency-list is not empty, use it in step 4. Page 40

Page 41 b a a b d b c c e h c c b e f e e j j i d g g f h i a b c d e f g h i j Figure1: The adjacency-list representation of Example 1(Slide 25) C1:a-b-c-a,

Page 42 d g b d b c c e h f h e f e e g j i g i a b c d e f g h i j After edge (a,b), (b, c) and (c, d) are used. C1:a-b-c-a, C2: bdefb, =>C3:abdefbca

a b c g h d e f g h i j g c c e h i e j j i C1:abca, C2:bdefb, =>C3:abdefbca. C4:eijhcge. C3+C4=>abdeijhcgefbca After edges (b, d) (d, e), (e, f), (f, b) are used. Page 43

Time complexity If it takes O( V ) time to add an edge to a circuit, then the time complexity is O( E V ). This can be done by using an adjacency list. The implementation is tricky Even O( V E ) algorithm is not trivial. Do not be disappointment if you do not completely understand the implementation The best algorithm takes O( E ) time. Euler Circuit: A complete example for Designing Algorithms. -formulation, design algorithm, analysis. Page 44

Euler Path A path which contains all edges in a graph G is called an Euler path of G. Deleting an edge in a graph having an Euler Circuit, we obtain an Euler path in the new graph. Deleting the edge Corollary: A graph G=(V,E) which has an Euler path has 2 vertices of odd degree. Page 45

Proof of the Corollary Suppose that a graph which has an Euler path starting at u and ending at v, where u v. Creating a new edge e joining u and v, we have an Euler circuit for the new graph G =(V, E {e}). From Theorem 1, all the vertices in G have even degree. Remove e. Then u and v are the only vertices of odd degree in G. (Nice argument, not required for exam.) Page 46

Application 1: In a map, two countries may share a common border. Given a map, we want to know if it is possible to find a tour starting from a country, going across each shared border once and come back to the starting country. A B A B D C D C Page 47

Application 2: Formulating a graph problem: Given a map containing Guizhou, Hunan, Jiangxi, Fujian, Guangxi, Guangdong, construct a graph such that if two provinces share some common boarder, then there is an edge connecting the two corresponding vertices. If we want to find a tour to visit each province once, then we need to find a Hamilton circuit in the graph. Page 48

Problem Solving: (only for those who are interested) (1) Prove that given a graph G=(V, E), one can always add some edges to the graph such that the new graph (multiple edges are allowed) has an Euler circuit. (2) Design an algorithm to add minimum number of edges to an input graph such that the resulting graph has an Euler circuit. (3) Prove the correctness of your algorithm. Page 49

Summary of Euler Circuit Algorithm Design a good algorithm needs two parts 1. Theorem, high level part 2. Implementation: low level part. Data structures are important. Our course emphasizes the first part and demonstrates the second part whenever possible. We will not emphasize too much about data structures. Page 50

Special Arrangements: We will have tutorials in Week 14 to makeup the tutorials in week 1 (if cancelled). To answer question for people from ALL groups. Page 51