Algorithms: Lecture 7. Chalmers University of Technology

Similar documents
Divide and Conquer 1

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

Copyright 2000, Kevin Wayne 1

3.1 Basic Definitions and Applications

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Ch5. Divide-and-Conquer

5. DIVIDE AND CONQUER I

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

3.1 Basic Definitions and Applications

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

Closest Pair of Points. Cormen et.al 33.4

5.4 Closest Pair of Points

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2019

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

Divide-Conquer-Glue Algorithms

Lecture 4 CS781 February 3, 2011

Divide-and-Conquer Algorithms

CSC Design and Analysis of Algorithms

Copyright 2000, Kevin Wayne 1

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CS 781 Advanced Algorithms 1 Winter 2011

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Algorithm Design and Analysis

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Divide-and-Conquer. Dr. Yingwu Zhu

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

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

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

Closest Pair of Points in the Plane. Closest pair of points. Closest Pair of Points. Closest Pair of Points

(Refer Slide Time: 01.26)

Divide and conquer algorithms. March 12, 2018 CSCI211 - Sprenkle. What is a recurrence rela&on? How can you compute D&C running &mes?

Solving Linear Recurrence Relations (8.2)

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Divide and Conquer. Algorithm Fall Semester

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

CSE 421 Algorithms: Divide and Conquer

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Algorithm Design Techniques part I

Master Theorem, Introduction to Graphs

Test 1 Review Questions with Solutions

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

CSE 373 Final Exam 3/14/06 Sample Solution

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

Quick Sort. CSE Data Structures May 15, 2002

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

Solving NP-hard Problems on Special Instances

Algorithms. Lecture Notes 5

Mergesort again. 1. Split the list into two equal parts

Algorithms Exam TIN093/DIT600

1 Closest Pair Problem

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

LECTURES 3 and 4: Flows and Matchings

Union Find. Data Structures and Algorithms Andrei Bulatov

Algorithms with numbers (1) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

CS 171: Introduction to Computer Science II. Quicksort

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

CSE 373: Data Structures and Algorithms. Graphs. Autumn Shrirang (Shri) Mare

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Mathematics Masters Examination

Lecture 3, Review of Algorithms. What is Algorithm?

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1 Definition of Reduction

Graphs Definitions. Gunnar Gotshalks. GraphDefinitions 1

CMSC 754 Computational Geometry 1

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

Lecture 2: Divide and Conquer

100 points total. CSE 3353 Homework 2 Spring 2013

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

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

Elementary maths for GMT. Algorithm analysis Part II

Algorithm Design (8) Graph Algorithms 1/2

SORTING AND SELECTION

Parallel Sorting Algorithms

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

CSE Winter 2015 Quiz 2 Solutions

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Subset sum problem and dynamic programming

15-451/651: Design & Analysis of Algorithms October 11, 2018 Lecture #13: Linear Programming I last changed: October 9, 2018

Divide and Conquer Algorithms

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

Multidimensional Divide and Conquer 2 Spatial Joins

CS6702 GRAPH THEORY AND APPLICATIONS 2 MARKS QUESTIONS AND ANSWERS

Transcription:

Algorithms: Lecture 7 Chalmers University of Technology

Today s Lecture Divide & Conquer Counting Inversions Closest Pair of Points Multiplication of large integers Intro to the forthcoming problems Graphs: Basic Definitions, Applications and Interesting Problems

Counting Inversions http://www.bogotobogo.com/algorithms/images/mergesort/inversion_counting_mergesort.png

Counting Inversions Given: a sequence (a 1,, a n ) of elements where an order relation < is defined. Goal: Count the inversions in this sequence. An inversion is a pair of elements where i < j but a i > a j. Find inversions in (2,4,1,3,5) Assume there is a sequence (1, 2, 3,, n), thus the given sequence is a permutation of it. What is it: degree of unsortedness of a given sequence. Dissimilarity of two sequences containing the same elements but in different order. 4

Counting Inversions RANKING: Music site tries to match your song preferences with others. You rank n songs. Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings. My rank: 1, 2,, n. Your rank: a 1, a 2,, a n. Songs i and j inverted if i < j, but a i > a j. Songs Me You A B C D E 1 2 3 4 5 1 3 4 2 5 Inversions 3-2, 4-2 Brute force is Trivial: check all O(n 2 ) pairs i and j. 5

Counting Inversions: Divide-and-Conquer Due to the vague similarity with Sorting, Divide-and-conquer should be applicable split the sequence into two equal halves, A and B. recursively count inversions in both A and B separately. count inversions between A and B, and return sum of the three quantities. T(n) = 2T(n / 2) +? 1 5 4 8 10 2 6 9 12 11 3 7 1 5 4 8 10 2 6 9 12 11 3 7 5 blue-blue inversions 8 green-green inversions 9 blue-green inversions 5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7 Total = 5 + 8 + 9 = 22. Need to replace? with O(n) to make it better than the Brute Force. Sorting the sequence while counting inversions? Can this help? OR Stupid idea as in Median Finding? Sorting AND Counting Inversions Simultaneously Does it sound good? Challenge: merge two sorted sequences A and B, and simultaneously count the inversions between A and B, still everything in O(n) time 6

Counting Inversions: Merge and Count Counting A-B inversions Assume both A and B are sorted. Proceed as in the Mergesort While merging whenever the next element copied into the merged sequence is from B, this element has inversions with exactly those elements of A not visited yet. 3 7 10 14 18 19 2 11 16 17 23 25 6 3 2 2 0 0 13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0 2 3 7 10 11 14 16 17 18 19 23 25 T(n) = 2T(n / 2) + O(n) implies O(n log n) 7

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 6 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves auxiliary array Total: 8

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 6 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 auxiliary array Total: 6 9

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 6 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 auxiliary array Total: 6 10

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 6 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 3 auxiliary array Total: 6 11

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 5 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 3 auxiliary array Total: 6 12

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 5 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 3 7 auxiliary array Total: 6 13

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 4 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 3 7 auxiliary array Total: 6 14

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 4 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 3 7 10 auxiliary array Total: 6 15

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 3 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 2 3 7 10 auxiliary array Total: 6 16

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 3 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 3 7 10 11 auxiliary array Total: 6 + 3 17

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 3 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 3 7 10 11 auxiliary array Total: 6 + 3 18

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 3 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 3 7 10 11 14 auxiliary array Total: 6 + 3 19

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 2 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 3 7 10 11 14 auxiliary array Total: 6 + 3 20

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 2 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 3 7 10 11 14 16 auxiliary array Total: 6 + 3 + 2 21

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 2 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 3 7 10 11 14 16 auxiliary array Total: 6 + 3 + 2 22

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 2 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 2 3 7 10 11 14 16 17 auxiliary array Total: 6 + 3 + 2 + 2 23

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 2 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 2 3 7 10 11 14 16 17 auxiliary array Total: 6 + 3 + 2 + 2 24

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 2 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 2 3 7 10 11 14 16 17 18 auxiliary array Total: 6 + 3 + 2 + 2 25

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 2 3 7 10 11 14 16 17 18 auxiliary array Total: 6 + 3 + 2 + 2 26

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 1 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 2 3 7 10 11 14 16 17 18 19 auxiliary array Total: 6 + 3 + 2 + 2 27

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. first half exhausted i = 0 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 2 3 7 10 11 14 16 17 18 19 auxiliary array Total: 6 + 3 + 2 + 2 28

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 0 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 0 2 3 7 10 11 14 16 17 18 19 23 auxiliary array Total: 6 + 3 + 2 + 2 + 0 29

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 0 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 0 2 3 7 10 11 14 16 17 18 19 23 auxiliary array Total: 6 + 3 + 2 + 2 + 0 30

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 0 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 0 0 2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array Total: 6 + 3 + 2 + 2 + 0 + 0 31

Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 0 3 7 10 14 18 19 2 11 16 17 23 25 two sorted halves 6 3 2 2 0 0 2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array Total: 6 + 3 + 2 + 2 + 0 + 0 = 13 32

Closest Pair of Points

Closest Pair of Points Given: Set of n points given as Cartesian coordinates in the plane Goal: Find a pair of points with minimum distance. Fundamental geometric primitive. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. Brute force: Check all pairs of points p and q with O(n 2 ) comparisons. Our aim: Using Divide and Conquer, T(n) = 2T(n / 2) + O(n) to get to O(n log n) 34

Closest Pair of Points How it works: Draw vertical line L so that roughly ½n points on each side. Points sorted by their x-coordinate Why sorting? find median directly in O(n) Take the median z of all x-values and put all points with coordinate x < z and x > z, respectively in two sets, S 1 and S 2 Compute the closest pairs in both S 1 and S 2 recursively. L 35

Closest Pair of Points How it works: Draw vertical line L so that roughly ½n points on each side. Points sorted by their x-coordinate Why sorting? find median directly in O(n) Take the median z of all x-values and put all points with coordinate x < z and x > z, respectively in two sets, S 1 and S 2 Compute the closest pairs in both S 1 and S 2 recursively. L 21 12 36

Closest Pair of Points How it works: Draw vertical line L so that roughly ½n points on each side. Points sorted by their x-coordinate Why sorting? find median directly in O(n) Take the median z of all x-values and put all points with coordinate x < z and x > z, respectively in two sets, S 1 and S 2 Compute the closest pairs in both S 1 and S 2 recursively. Compute closest pairs where one point is in S 1 while the other in S 2??? L 21 12 37

Closest Pair of Points Let d 1 be the distance between the closest pair in S 1 Similarly d 2 for the set S 2 d := min (d 1, d 2 ) L 21 12 38

Closest Pair of Points Find closest pair with one point in S 1 while the other in S 2 Because we already have closest pair with distance d The candidates for such pairs of points are in a stripe of breadth d on both sides of the separating line L Consider the pairs with one point in each set, assuming that distance < d. L 21 12 d = min(12, 21) d 39

Closest Pair of Points Find closest pair with one point in S 1 while the other in S 2 Sort points in 2d-strip by their y coordinate. Only check distances of those within 11 positions in sorted list! (d is 12) Managed everything in O(n) for the conquer step. Finally, standard recurrence T(n) = 2T(n / 2) + O(n) with solution O(n log n) 7 L 6 4 5 21 12 3 d = min(12, 21) 2 1 d 40

Multiplication of large integers 2095067093034680994318596846868779409766717133476767930 X 5920175091777634709677679342929097012308956679993010921 9715480283945084383094856701043643845790217965702956767 + 1242431098234099057329075097179898430928779579277597977

Multiplication of large integers a, b are both n-digit integers If we use the brute-force approach to compute c = a * b, what is the time efficiency? Remember the School Algorithm addition of n integers, each with O(n) digits requires O (n 2 ) Already discussed in Lecture 1. Divide and Conquer: Can we do better? Split the decimal representations of the factors a and b into two halves, and to multiply, use the distributive law. 3 6 = 3 (2 + 4) = 3 2 + 3 4 42

Multiplication of large integers a = a 1 a 0 and b = b 1 b 0 c = a * b = (a 1 10 n/2 + a 0 ) * (b 1 10 n/2 + b 0 ) =(a 1 * b 1 )10 n + (a 1 * b 0 + a 0 * b 1 )10 n/2 + (a 0 * b 0 ) For instance: a = 123456, b = 117933: Then c = a * b = (123*10 3 +456)*(117*10 3 +933) =(123 * 117)10 6 + (123 * 933 + 456 * 117)10 3 + (456 * 933) What we achieve: multiplication of n-digit numbers is reduced to: several multiplications of n/2-digit numbers and some additions In fact: we need 4 multiplications involving a 1,a 0,b 1, and b 0 Multiplications with 10 n and 10 n/2 are trivial: Append the required number of 0s. 43

Multiplication of large integers a = a 1 a 0 and b = b 1 b 0 c = a * b = (a 1 10 n/2 + a 0 ) * (b 1 10 n/2 + b 0 ) =(a 1 * b 1 )10 n + (a 1 * b 0 + a 0 * b 1 )10 n/2 + (a 0 * b 0 ) Solve by recursive application of the above: At every recursion step: problem reduces to 4 recursive calls AND addition Clearly: T(n) = 4T(n / 2) + O(n), a = 4, b = 2, k = 1. Master Theorem implies: T(n) = O(n log 2 4 ) = O(n 2 ) Still same like Brute Force we need to avoid 4 recursive calls at each level 44

Multiplication of large integers a = a 1 a 0 and b = b 1 b 0 c = a * b = (a 1 10 n/2 + a 0 ) * (b 1 10 n/2 + b 0 ) =(a 1 * b 1 )10 n + (a 1 * b 0 + a 0 * b 1 )10 n/2 + (a 0 * b 0 ) Can we achieve? multiplication of n-digit numbers by three multiplications of n/2-digit numbers + O(n) <presenting the idea using the rectangle example> 45

Multiplication of large integers a = a 1 a 0 and b = b 1 b 0 c = a * b We are aiming at: c = c 2 10 n + c 1 10 n/2 + c 0, where computing each of c 2, c 1 and c 0 requires ONE multiplication This is achievable as follows: c = (a 1 * b 1 )10 n + (a 1 * b 0 + a 0 * b 1 )10 n/2 + (a 0 * b 0 ) =c 2 10 n + c 1 10 n/2 + c 0, where c 2 = a 1 * b 1 is the product of their first halves c 0 = a 0 * b 0 is the product of their second halves c 1 = (a 1 + a 0 ) * (b 1 + b 0 ) (c 2 + c 0 ) is the product of the sum of the a s halves and the sum of the b s halves minus the sum of c 2 and c 0. 46

Multiplication of large integers Finally c =c 2 10 n + c 1 10 n/2 + c 0, where c 2 = a 1 * b 1 c 0 = a 0 * b 0 c 1 = (a 1 + a 0 ) * (b 1 + b 0 ) (c 2 + c 0 ) Clearly: T(n) = 3T(n / 2) + O(n), a = 3, b = 2, k = 1. Master Theorem implies: T(n) = O(n log 2 3 ) = O(n 1.59 ) far better than O(n 2 ) Caution: Factors (a 1 + a 0 ) and (b 1 + b 0 ) may have (n/2) + 1 digits Split off the first digit and have recursive calls of (n/2) Can only cause O(n) extra work, won t affect the time bound Acceleration takes effect only for rather large n (more than some 100 digits) Recursive calls being the overhead 47

Graphs Basic Definitions, Applications and Interesting Problems

Graphs Graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures binary relationships between objects (nodes). Symmetric relationships: G is undirected, all edges (u, v) are unordered: (u, v) and (v, u) are identical Asymmetric relationships : G is directed, All edges (u, v) are ordered: (u, v) and (v, u) are different Graph size parameters: n = V, m = E. A node and an edge are incident if the edge contains this node. Two vertices(nodes) joined by an edge are called adjacent. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11 49

Some Graph Applications Graph Nodes Edges transportation street intersections highways communication computers fiber optic cables World Wide Web web pages hyperlinks social people relationships food web species predator-prey software systems functions function calls scheduling tasks precedence constraints circuits gates wires 50

World Wide Web Web graph. Node: web page. Edge: hyperlink from one page to another. cnn.com netscape.com novell.com cnnsi.com timewarner.com hbo.com sorpranos.com 51

Social network Social network graph. Node: people. Edge: relationship between two people. Reference: Valdis Krebs, http://www.firstmonday.org/issues/issue7_4/krebs 52

Ecological Food Web Food web graph. Node = species. Edge = from prey to predator. (victim to killer) Reference: http://www.twingroves.district96.k12.il.us/wetlands/salamander/salgraphics/salfoodweb.giff 53

Graph Representation: Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge, 0 otherwise Two representations of each edge. Space proportional to n 2. Checking if (u, v) is an edge takes O(1) time. Identifying all edges takes O(n 2 ) time. 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 54

Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. Two representations of each edge. degree = number of incident edges Space proportional to m + n. Checking if (u, v) is an edge takes O(deg(u)) time. Identifying all edges takes O(m + n) time. In most graph algorithms, adjacency lists are preferable, as they do not waste space for non-edges. Nodes in Directed graphs: in-degree: the number of incoming edges out-degree: the number of outgoing edges. 1 2 3 2 3 1 3 4 5 1 2 5 7 8 4 2 5 5 6 2 3 4 6 5 7 3 8 8 3 7 55

Graph Problems: Clique Given: An Undirected Graph G. Goal: Find a clique of maximum size. a subset K of nodes such that there is an edge between any two nodes in K Motivation: The graph models an interaction network (persons in a social network, proteins in a living cell, etc.), where an edge means some close relation between two nodes. We may wish to identify big groups of pairwise interacting nodes. http://scienceblogs.com/goodmath/wp-content/blogs.dir/476/files/2012/04/i-24b2db50d71be775f98de9a464113aca-maximal-cliques.jpg 56

Graph Problems: Independent Set Given: An Undirected Graph G. Goal: Find and independent set of maximum size. subset of nodes such that no two joined by an edge Motivation: The graph models conflicts between items, and we wish to select as many as possible items conflict-free. 1 2 3 4 5 6 7 57

Graph Problems: Vertex Cover Given: An Undirected Graph G. Goal: Find a vertex cover of minimum size. a subset of nodes such that every edge of G has at least one of its two nodes in it Motivation: How can we place a minimum number of guards in a museum building so that they can watch all corridors? http://faculty.ycp.edu/~dbabcock/pastcourses/cs360/lectures/images/lecture29/approxvert4.png 58

Graph Problems: Difficulty How difficult is to solve these Graph problems (some hints): Maximal Clique: Find a clique of maximum size. a subset K of nodes such that there is an edge between any two nodes in K k = 2 is trivial ( every pair of adjacent nodes ) But, we need to consider all possible k. Furthermore, for every k, we need to check all k-subsets of nodes. In other words, we need to check all possible 2 n subsets of the node set V. n could be very large This 2 n is not special here, same applies to e.g., Interval Scheduling The difference is Our Algorithm Design Approaches seem to fail here Next Week: Going to be hard for whom? Of course for these problems. However, to fight against a hard opponent, we need to work hard! 59