1 / 29

Data Structure

Data Structure. Lecture 10 Thursday, 28 Aug 2005. Algorithms and Complexity. Examples of some well-known algorithms: Euclid algorithm for GCD, Gaussian elimination, quick sort, fast Fourier transform Complexity: how long does it take to do a computation, and how much memory do we use.

Download Presentation

Data Structure

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. Data Structure Lecture 10 Thursday, 28 Aug 2005

  2. Algorithms and Complexity • Examples of some well-known algorithms: Euclid algorithm for GCD, Gaussian elimination, quick sort, fast Fourier transform • Complexity: how long does it take to do a computation, and how much memory do we use

  3. Big-O notation • O(g(n)) is a set such that if f(n) belong to the set, then 0 < f(n) <= c g(n) for all n > n0, for some constant c and n0. Example 2n2 + 3n = O(n2)

  4. Examples of Algorithm’s Running Time • Gaussian elimination – O(n3) • Quick sort – O(n log n) • Minimum spanning trees – O(E log V) • Traveling-salesman problem – O(exp(c n)) ?

  5. Sorting (1) • Simple sorting, for each i, compare it with the rest of j, swap if it is in the wrong order. for(i = 0, i < n; ++i) { for(j = i+1; j < n; ++j) { if(a[i] < a[j]) { swap(i,j); } } } // What is the complexity?

  6. Quick Sort • Divide the array in two groups, such that all members of the first group is smaller than the second group • Sort each group • Combine the results

  7. Quick Sort Pseudo-code Quicksort(A[ ], p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); } }

  8. Pseudo-code Continued Partition(A[ ], p, r) { x = A[p]; i = p –1; j = r + 1; while(1) { do { --j; } while(A[j] > x); do { ++i; } while (A[i] < x); if(i<j) { swap(i,j); } else { return j; } }

  9. The Working of Code i i 3 3 2 6 4 1 5 7 5 3 2 6 4 1 3 7 j j i j 5 3 2 6 4 1 3 7 3 3 2 1 4 6 5 7 i j j i 3 3 2 6 4 1 5 7 i j

  10. Data Structure in C++ • Arrays – Useful when the structure of the problem can be presented by regular grids, e.g., temperature field in a cube. • Dynamical data structure – the data and relation among them (topology) are not fixed, e.g., road between cities, trees, graphs

  11. Linked List Doubly linked list

  12. Trees root leaf

  13. Graph vertex or node edge or link Directed graph vs undirected graph

  14. Data Structure • The basic data type to represent the tree or graph structure is a node • struct node { type for data; one or more pointers; }

  15. Adjacency Matrix/List • For a graph, we can represent it as an array of node, each node has a linked list of other nodes that this node is linked to • Or we can use a two-dimensional array, information on the link between node i and j is stored at entry (i,j)

  16. Shortest Path Problem Imagine we have n cities and and distances dij between city i and j. Find the shortest path that goes from a given city a to a given city b. We do not assume that dij = dji.

  17. Enumerations • Starting from city a, we enumerate all the cities that can be reached from a, and keep track of the distances traveled. For each of the cities, we enumerate all the cities that can be reached in two hops, and so on, until we find city b. The amount of computation will be exponential.

  18. Dijkstra’s Algorithm Dijkstra(G, w, s) Initialize-single-source(G,s); S = empty set; Q = V[G]; While Q is not empty { u = Extract-Min(Q); S = S  {u}; for each vertex v in Adj[u] { relax(u, v, w); } }

  19. Data Structure in Dijkstra • G – the graph, represented in some way (e.g. Adjacency list) • w – the distance (weight) for each edge (u, v) • s (small s) – the starting vertex (source) • S (big S) – a set of vertices whose final shortest path from s have already been determined • Q – set of remaining vertices, Q  S = V

  20. Functions Initialize-… Initialize-single-source(G,s) { for each vertex v in V(G) { d[v] = infinity; // upper bound to v p[v] = NIL; // previous vertex } d[s] = 0; }

  21. Function Relax(…) Relax(u, v, w) { if (d[v] > d[u] + w(u, v)) { d[v] = d[u] + w(u,v); p[v] = u; } // if it is shorter by going from } // u to v, do it

  22. Extract-Min(Q) • Extract-Min(Q) takes out a vertex u from the set Q with smallest distance d[u]. The cardinality (size) of the set |Q| is thus reduced by 1

  23. Execution of Dijkstra’s Algorithm 1   10 9 Next u 2 3 4 6 0 7  5  2 (a)

  24. Execution of Dijkstra’s Algorithm 1 In set Q 10  10 9 In set S 2 3 4 6 0 7  5 5 2 (b)

  25. Execution of Dijkstra’s Algorithm 1 8 14 10 9 2 3 4 6 0 7 7 5 5 Next u 2 (c)

  26. Execution of Dijkstra’s Algorithm 1 8 13 10 9 2 3 4 6 0 7 7 5 5 2 (d)

  27. Execution of Dijkstra’s Algorithm 1 8 9 10 9 2 3 4 6 0 7 7 5 5 2 (e)

  28. Execution of Dijkstra’s Algorithm 1 8 9 10 9 2 3 4 6 Use predecessor to find the pass 0 7 7 5 5 2 (f)

  29. Reference • “Introduction to Algorithms” by Cormen, Leiserson, and Rivest

More Related