8 views

Uploaded by Kassım Kıngalu

Analysis of Algorithms
Chapter 9.1, 9.2, 9.3, 9.4

- UT Dallas Syllabus for cs6381.081 06u taught by Ramaswamy Chandrasekaran (chandra)
- greedy
- 09TSPbacktrack
- Bellman Ford 4up
- Aplicatie VBA dizertatie
- Dynamic Programming
- Important Algorithms MAKAUT(former WBUT)
- Nln Live Icmc Smc 2014 Proceedings
- mine system analysis_Network Analysis
- 24052007 Further Notebook (2)
- 187985328 Dynamic Programming TopCoder Tutorials
- Lecture Slides-CPlusPlus LectureNotes Module2
- Lect 0919
- Wireless Optical Networks
- Mid Exam Key
- Lec2 Network Flows
- daa_ lesson plan 2016.doc
- Dynamic
- Cp prog Net
- 99 Holy Names of Allah

You are on page 1of 52

ROAD MAP

Greedy Technique

Huffman Trees

Greedy Technique

Each expanding a partially constructed solution Until a complete solution is reached but, not all possible solutions are explored

3

Greedy Technique

On each decision step the choice should be Feasible

has to satisfy the problems constraints has to be the best local choice

Locally optimal

Irrevocable

4

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) if feasible ( solution, x ) solution = solution U {x} return solution }

5

Greedy Technique

In each step, greedy technique suggests a greedy selection of the best alternative avaliable

Feasible decision Locally optimal decision Hope to yield a globally optimal solution

Greedy technique does not give the optimal solution for all problems

6

Optimal solutions: change making for normal coin denominations minimum spanning tree (MST) single-source shortest paths simple scheduling problems Huffman codes

Approximations: traveling salesman problem (TSP) knapsack problem other combinatorial optimization problems

7

Change-Making Problem

Given unlimited amounts of coins of denominations d1 > > dm , give change for amount n with the least number of coins

Example: d1 = 25c, d2 =10c, d3 = 5c, d4 = 1c and n = 48c Greedy solution: Greedy solution is optimal for any amount and normal set of denominations may not be optimal for arbitrary coin denominations

Given :

wi : weight of object i m : capacity of knapsack pi : profit of all of i is taken

Find:

xi : fraction of i taken

Feasibility:

x w

i 1 i

Optimality: maximize

x p

i 1 i

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) if feasible ( solution, x ) solution = solution U {x} return solution }

10

Knapsack Problem

Algorithm Knapsack (m,n) for i = 1 to n x(i) = 0 for i = 1 to n select the object (j) with largest unit value if (w[j] < m) x[j] = 1.0 m = m w[j] else x[j] = m/w[j] break

11

ROAD MAP

Greedy Technique

Huffman Trees

12

Problem Instance:

Definition:

A spanning tree of a connected graph is its connected acyclic subgraph A minimum spanning tree of a weighted connected graph is its spanning tree of the smallest weight weight of a tree is defined as the sum of the weights on all its edges

Feasible Solution:

A spanning tree G of G

G' (V , E ' )

E' E

13

Objective function :

C (G' )

eG '

C (e)

Optimum Solution :

14

15

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) if feasible ( solution, x ) solution = solution U {x} return solution }

16

Prims Algorithm

Greedy choice :

Choose minimum cost edge add it to the subgraph

17

Prims Algorithm

18

Prims Algorithm

Approach :

1. Each vertex j keeps near[j] T (current tree) where cost(j,near[j]) is minimum 2. near[j] = 0 if j T = if there is no egde between j and T 3. Use a heap to select minimum of all edges

19

20

21

22

Prims Algorithm

1. 2.

3.

4. 5.

6.

7. 8. 9. 10. 11. 12. 13. 14. 15.

Initialize S with the start vertex, s, and VS with the remaining vertices for all v in V S if there is an edge (s, v) Set cost[v] to w(s, v) Set next[v] to s else Set cost[v] to Set next[v] to NULL while V S is not empty for all u in V S, find the smallest cost[u] Remove u from V S and add it to S Insert the edge (u, next[u]) into the spanning tree. for all v adjacent to u in V S if w(u, v) < cost[v] Set cost[v] to w(u, v) Set next[v] to u.

23

Prims Algorithm

Analysis : How efficient is Prims algorithm ?

graph is represented by its weight matrix unordered array is used graph is represented by adjacency list priority queue such as a min-heap is used

24

Kruskals Algorithm

initially contains all the vertices but no edges always acyclic not necessarily connected

25

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) if feasible ( solution, x ) solution = solution U {x} return solution }

26

Kruskals Algorithm

27

28

29

30

ROAD MAP

Greedy Technique

31

Greedy Technique

Each expanding a partially constructed solution Until a complete solution is reached Feasible : has to satisfy the problems constraints Locally optimal: has to be the best local choice Irrevocable : once made, can not be changed

32

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) if feasible ( solution, x ) solution = solution U {x} return solution }

33

Definition:

For a given vertex called source in a weighted connected graph, find shortest paths to all other vertices in the graph

34

Dijkstras Algorithm

Idea :

Incrementally add nodes to an empty tree Each time add a node that has the smallest path length

Approach : 1. S = { } 2. Initialize dist [v] for all v 3. Insert v with min dist[v] in T 4. Update dist[w] for all w not in S

35

Dijkstras Algorithm

36

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) if feasible ( solution, x ) solution = solution U {x} return solution }

37

38

39

40

Dijkstras Algorithm

Analysis :

Time efficiency depends on the data structure used for priority queue and for representing an input graph itself For graphs represented by their weight matrix and priority queue implemented as an unordered array, efficiency is in (|V|2)

For graphs represented by their adjacency list and priority queue implemented as a min-heap efficiency is in O(|E|log|V|) A better upper bound for both Prim and Dijkstras algorithm can be achieved, if Fibonacci heap is used

41

ROAD MAP

Greedy Technique

Huffman Trees

42

Encoding Text

Suppose we have to encode a text that comprises characters from some n-character alphabet by assigning to each of the texts characters some sequence of bits called codeword We can use a fixed-encoding that assigns to each character

Good if each character has same frequency What if some characters are more frequent than others

43

Encoding Text

EX: The number of bits in the encoding of 100 characters long text

b 13 ... 101

c 12 100

d 16 111

e 9

44

Prefix Codes

Change each character with its codeword Start with the first bit Find the codeword A unique codeword can be found prefix code Continue with the bits following the codeword

Encoding

Decoding

45

Prefix Codes

b ... 101

c 100

46

Huffman Codes

Given: The characters and their frequencies Find: The coding tree Cost : Minimize the cost

cC

47

Huffman Codes

48

Huffman Codes

Approach :

1. Q = forest of one-node trees // initialize n one-node trees; // label the nodes with the characters // label the trees with the frequencies of the chars 2. for i=1 to n-1 3. x = select the least freq tree in Q & delete 4. y = select the least freq tree in Q & delete 5. z = new tree 6. zleft = x and zright = y 7. f(z) = f(x) + f(y) 8. Insert z into Q

49

Greedy Technique

Greedy Algorithm ( a [ 1 .. N ] ) { solution = for i = 1 to n x = select (a) is feasible ( solution, x ) solution = solution U {x} return solution }

50

Consider five characters {A,B,C,D,-} with following occurrence probabilities

51

52

- UT Dallas Syllabus for cs6381.081 06u taught by Ramaswamy Chandrasekaran (chandra)Uploaded byUT Dallas Provost's Technology Group
- greedyUploaded bySarbani Dasgupts
- 09TSPbacktrackUploaded byHoàng Đạo
- Bellman Ford 4upUploaded byMr.SANTANU BANERJEE
- Aplicatie VBA dizertatieUploaded byOvidiu Harpalete
- Dynamic ProgrammingUploaded byGiri Saranu
- Important Algorithms MAKAUT(former WBUT)Uploaded bysrjmukherjee2
- Nln Live Icmc Smc 2014 ProceedingsUploaded byThan Van Nispen
- mine system analysis_Network AnalysisUploaded byAli Çakır
- 24052007 Further Notebook (2)Uploaded byJames Nola
- 187985328 Dynamic Programming TopCoder TutorialsUploaded byMilos Jevtovic
- Lecture Slides-CPlusPlus LectureNotes Module2Uploaded byPranay Bansal
- Lect 0919Uploaded byBelal Ahmed
- Wireless Optical NetworksUploaded byPrasenjit Malakar
- Mid Exam KeyUploaded byclaudioluciolopes
- Lec2 Network FlowsUploaded byChandra Mouli
- daa_ lesson plan 2016.docUploaded byVjay Narain
- DynamicUploaded byBhavini Rajendrakumar Bhatt
- Cp prog NetUploaded bynivireddy

- Traveling Salesman ProblemUploaded byBrian Xistos
- Ch5Uploaded byjackaccyou
- Algo Mcq CompiledUploaded bysny2ksa
- Greedy TechniquesUploaded bySiddhant Singh
- Design and Analysis of Algorithms Course NotesUploaded byshardapatel
- Final ExercisesUploaded byAshish Darji
- Greedy (1).pdfUploaded byAdit Bhardwaj
- Billey_Discrete_Mathematical_Modeling(math381_lecture_notes_Winter_2011_U_Washington).pdfUploaded byRicardo Marquez Hdez
- The Floyd-Warshall AlgorithmUploaded bySyed Muhammad Junaid Hassan
- Topological sort in DelphiUploaded byBrummer
- 4th Semester 5 Years Questions - CSE - NUUploaded byKhokanuzzaman Khokan
- Design and Analysis of AlgorithmsUploaded bysuri5501
- MCQ-0Uploaded byanshulvyas23
- [IJIT-V3I3P9]:S. Lana Fernando, A. SebastianUploaded byIJITJournals
- D2 MockUploaded bySupun Mannapperuma
- Unit 4 ds.pdfUploaded byNivitha
- Cse-IV-Design and Analysis of Algorithms [10cs43]-NotesUploaded byRajesh Kanna
- Greedy Method MaterialUploaded byAmaranatha Reddy P
- Unit II(Greedy Algorithms)Uploaded byAshi Krey
- Finite State AutomataUploaded byTanupriya Chaubey
- Maths explosionUploaded bySallyAce
- DAA 2marks With AnswersUploaded bymiraclesuresh
- Ads Lab RecordUploaded bySabareesh Balakrishnan
- CS1252 – Design and Analysis of Algorithms.pdfUploaded byNaresh Babu Merugu
- qUploaded byamit
- [IJCST-V6I2P7]:Jogamohan MedakUploaded byEighthSenseGroup
- Chapter 5 - TreesUploaded bytrevorsum123
- DAA MINIPROJECTUploaded byVivek Singh
- Greedy Graphs Prim Kruskal DijstkraUploaded byawadhesh786
- CS506_Week_3.pdfUploaded byShafaq Khan