1 / 32

Military Technical Academy Data Structures and Algorithms

Military Technical Academy Data Structures and Algorithms. Lecturer: Dr. Nguyen Nam Hong Tel: 04 8781 437 Mob: 0912 312 816 Email: nguyennamhong2003@yahoo.com.au Lecture 17. Linked List Applications. Lecture 17. Linked List Applications (1/2). References:

rune
Download Presentation

Military Technical Academy Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Military Technical Academy Data Structures and Algorithms Lecturer: Dr. Nguyen Nam Hong Tel: 04 8781 437 Mob: 0912 312 816 Email: nguyennamhong2003@yahoo.com.au Lecture 17. Linked List Applications Dr. Nguyen Nam Hong, Le Quy Don Technical University

  2. Lecture 17. Linked List Applications (1/2) References: 1. Deshpande Kakde: C and Data structures.chm Chapter 20: Linked Lists 2. Elliz Horowitz – Fundamentals of Data Structures.chm Chapter 4: Linked Lists 3. Kyle Loudon: Mastering Algorithms with C.chm Chapter 5 Linked Lists Dr. Nguyen Nam Hong, Le Quy Don Technical University

  3. Lecture 17. Linked List Applications (2/2) Contents: 17.1. Topological Sort 17.2. Critical Path 17.3. Sparse Polynomials 17.4. Other Application Dr. Nguyen Nam Hong, Le Quy Don Technical University

  4. 17.1. Topological Sort Some Real Problems Partially Order Relation Topological Sort Algorithm Topological Sort Example Dr. Nguyen Nam Hong, Le Quy Don Technical University

  5. Some Real Problems 1. Dictionary Some worda are defined based on some other words. 2. Project Planning Some tasks must be completed before some other tasks. 3. Education Programs Some subjects must be learnt before some other subjects. Dr. Nguyen Nam Hong, Le Quy Don Technical University

  6. Partially Order Relation A relation R on a set X is called a partial order if R has the following properties 1. Reflexive x R x for every x  X 2. Antisymmetric if x R y and y R x then x = y 3. Transitive if x R y and y R z then x R z Dr. Nguyen Nam Hong, Le Quy Don Technical University

  7. Topological Sort Declaration Type Node Value: Data PreNode[]: Data PreNum: Int Next: Node End Type Dr. Nguyen Nam Hong, Le Quy Don Technical University

  8. Topological Sort Algorithm (1/3) OList: Node ‘Original List SList: Node ‘Sorted List ptr: Node ‘working pointer Sub TopoSort() While OList<>Null TopoCycle(OList) Loop End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University

  9. Topological Sort Algorithm (2/3) Sub TopoCycle(ptr: Node) If ptr.PreNum=0 Then Delete(OList,ptr) Append(SList,ptr) AdjustPreNum ptr.Value Else ptr=ptr.next TopoCycle(ptr.next) End If End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University

  10. Topological Sort Algorithm (3/3) Sub AdjustPreNum(vl:Data) ptr=OList While ptr<>Null If vl in ptr.PreNode Then ptr.PreNum=ptr.PreNum-1 End If Loop End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University

  11. C E F B A D G Topological Sort Example (1/2) Possible Task Order: C D A G E F B Dr. Nguyen Nam Hong, Le Quy Don Technical University

  12. Topological Sort Example (2/2) Dr. Nguyen Nam Hong, Le Quy Don Technical University

  13. 17.2. Critical Paths State the Problem Critical Path Terminologies Critical Path Algorithm Critical Path Example Dr. Nguyen Nam Hong, Le Quy Don Technical University

  14. State the Problem • A set of tasks must be complete • Some tasks are prequisites for others • If each task requires a given amount of time • Different task may be done at the same time • How much time will be required to complete the entire set of tasks • Use Topological Sort to get the order of tasks Dr. Nguyen Nam Hong, Le Quy Don Technical University

  15. Critical Path Terminologies • T(x): The time required by x • ET(x): Earliest Finishing Time for x • LT(x): Last Time to complete x • FT: Finish Time for all task • FT=max(ET(x)) for all task x • Critical Tasks: ET(x)=LT(x) • Critical Paths: • contains all Critical Tasks • starting from a task with no prequisites • ending at a task that is finished at time FT Dr. Nguyen Nam Hong, Le Quy Don Technical University

  16. Critical Path Algorithm (1/2) Func ET(x: Node) If x has no Prequisite Then ET=T(x) Else ET=T(x)+max(ET(y)) max is taken for all prequsites y of x End If End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University

  17. Critical Path Algorithm (2/2) Func LT(x:Data) If x is not a PreQuisite for any task Then LT=FT Else LT= min(LT(y)-T(y)) for variable y for which x is a PreRequisite End If End Sub Dr. Nguyen Nam Hong, Le Quy Don Technical University

  18. Critical Path Example (1/2) Possible Task Order: C D A G E F B C E F B A D G Dr. Nguyen Nam Hong, Le Quy Don Technical University

  19. Critical Path Example (2/2) Dr. Nguyen Nam Hong, Le Quy Don Technical University

  20. 17.3. Sparse Polynomials Represent Polynomials by Arrays Add Array-Based Polynomials Multiply Array-Based Polynomials Representing Sparse Polynomials Sparse Polynomial Calculations Add Sparse Polynomials Multiply Sparse Polynomials Dr. Nguyen Nam Hong, Le Quy Don Technical University

  21. Represent Polynomials by Arrays • Representation • Use an array to store coefficients • Item Index is equal to the exponent of variable • Examples • Y1 = 5 + 7x – 8x3 + 4x5 -> arr1 = [5, 7, 0, -8, 0, 4] (6 Items) • Y2 = 1 + x99 -> arr2 = [1, 0, 0, …, 0, 0, 1] (100 Items) Dr. Nguyen Nam Hong, Le Quy Don Technical University

  22. Add Array-Based Polynomials • Denote p = max(n,m), q = min(n,m) A = [a[i], 0 <= i <= n] B = [b[j], 0 <= j <= m] C = [c[k], 0 <= k <= p] = A + B • For 0 <= k <= q Compute C[k] = A[k]+B[k] • For q < k <= p Compute C[k] = a[k] if n > m C[k] = b[k] if n < m Dr. Nguyen Nam Hong, Le Quy Don Technical University

  23. Multiply Array-Based Polynomials • Denote p = n+m A = [a[i], 0 <= i <= n] B = [b[j], 0 <= j <= m] C = [c[k], 0 <= k <= p] = A * B • For 0 <= k <= p compute • C[k] = sum(0<=j<=k,A[j]*B[k-j]) • Note that C is Convolution of A and B Dr. Nguyen Nam Hong, Le Quy Don Technical University

  24. Representing Sparse Polynomials • Use Linked List to Save Space • One Node for a Term with nonzero Coefficient • A Node Contains Coefficient and Exponent of the Term • Examples: • Y1 = 5 + 7x – 8x3 + 4x5 -> List1 = [5,0]→[7,1]→[-8,3]→[4,5] • Y2 = 1 + x99 -> List2 = [1, 0]→[1,99] Dr. Nguyen Nam Hong, Le Quy Don Technical University

  25. Add Sparse Polynomials (1/3) • Addition operator • Adds coefficients of like degrees • Must traverse the two addend polynomials • Requires temporary pointers for each polynomial (the addends and the resulting sum) Dr. Nguyen Nam Hong, Le Quy Don Technical University

  26. Add Sparse Polynomials (2/3) • As traversal takes place • Compare exponents • If different, node with smaller exponent and its coefficient attached to result polynomial • If exponents same, coefficients added, new corresponding node attached to result polynomial Dr. Nguyen Nam Hong, Le Quy Don Technical University

  27. Add Sparse Polynomials (3/3) • Examples A = 4x10 - 2x3 + 6  A = [4,10]→[-2,3]→[6,0] B = -6x10 + 8x6 + 2x3 + 5x  B = [-6,10]→[8,6]→[2,3]→[5,1] • Result C = -2x10 + 8x6 + 5x + 6  C = [-2,10]→[8,6]→[5,1]→[6,0] Dr. Nguyen Nam Hong, Le Quy Don Technical University

  28. Multiply Sparse Polynomials (1/4) • Multiplication operator • Multiplies Coefficients and Adds Exponents • Create new Node with Result Coefficient and Exponent • Add new Node to the Result List • Must traverse the two multiplied polynomials • Requires temporary pointers for each polynomial (the multipliers and the resulting multiplication) Dr. Nguyen Nam Hong, Le Quy Don Technical University

  29. Multiply Sparse Polynomials (2/4) • Add new Node to the Result List • If the Result List has no Node with the new exponent: Append the Node to the Result List • If the Result List has a Node with the new Exponent: Add the Node Coefficient to Coeeficient of that Node in the Result List Dr. Nguyen Nam Hong, Le Quy Don Technical University

  30. Multiply Sparse Polynomials (3/4) • Examples A = 4x10 - 2x3 + 6  A = [4,10]→[-2,3]→[6,0] B = -6x10 + 8x6 + 2x3 + 5x  B = [-6,10]→[8,6]→[2,3]→[5,1] Dr. Nguyen Nam Hong, Le Quy Don Technical University

  31. Multiply Sparse Polynomials (4/4) • Result C = -24x20 + 32x16 + 20x13 + 20x11 - 36x10 - 16x9 + 44x6 - 10x4 + 12x3 +30x  C=[-24,20]→[32,16]→[20,13]→[20,11] →[-36,10]→[16,9]→[44,6] →[-10,4] →[12,3]→[30,1] Dr. Nguyen Nam Hong, Le Quy Don Technical University

  32. 17.4. Other Linked List Applications • Sparse Matrix Representation • Similar to Sparse Polynomial • Dynamic Stack Implementation • Dynamic Queue Implementation • Tree Implementation • Note: Use Link List save space but it is more complex implementation Dr. Nguyen Nam Hong, Le Quy Don Technical University

More Related