Download presentation
Presentation is loading. Please wait.
Published byJane Austin Modified over 10 years ago
1
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures (Ch. 6) n Balanced Search Trees: general search structures (Ch. 4.1-4.5) n Union-Find data structure (Ch. 8.1–8.5) n Graphs: Representations and basic algorithms Topological Sort (Ch. 9.1-9.2) Minimum spanning trees (Ch. 9.5) Shortest-path algorithms (Ch. 9.3.2) n B-Trees: External-Memory data structures (Ch. 4.7) n kD-Trees: Multi-Dimensional data structures (Ch. 12.6) n Misc.: Streaming data, randomization
2
1 Graphs: directed and undirected and labeled n A GRAPH G = (V, E) where V is a set of vertices (nodes), and E is a set of edges (arcs), i.e., pairs of vertices n D IRECTED graphs (digraphs): edges are directed n U NDIRECTED graphs: edges are undirected n Labeled graphs: edges have labels, directed or undirected a d e b cf j h g i a b c ed
3
2 Adjacency matrix n G=(V,E) and V={v 1,…,v N } n A(i,j) = 1 if (v i,v j ) is in E, A(i,j) = 0 otherwise Space: O(N 2 ) Edge insertion/deletion: O(1) Find all adjacent vertices to a vertex: O(N) abcde a111 b111 c111 d111 e11 abcdefghij a11 b11 c111 d1 e f1 g11 h11 i1 j1
4
3 Adjacency lists n All vertices adjacent to v i are stored in the list adj(v i ) Space: O(N+|E|) Edge insertion/deletions Find all adjacent vertices to a vertex adj(a)= (b, d, e) adj(b)= (a, c, d) adj(c)= (b, d, e) adj(d)= (a, b, c) adj(e)= (a, c) adj(a)= (b, d) adj(b)= (c, f) adj(c)= (a, d, e) adj(d)= (e) adj(e)= () adj(f)= (c) adj(g)= (f, h) adj(h)= (f, j) adj(i)= (h) adj(j)= (I)
5
4 Paths and simple paths n A PATH is a sequence of vertices v 1, …, v k where each (v i, v i+1 ) is an edge n S IMPLE path: v i ¹ v j, except that v 1 may be equal to v k n Path LENGTH : number of edges in the path n A CYCLE : a path v 1, …, v k such that v 1 = v k n Examples: abdcea (undirected), hjihfcad (directed) a b c de a d e b cf j h g i
6
5 Topological sort n Ordering of vertices in a digraph such that if there is an edge from v i to v j, v i appears before v j in the ordering n Complexity: O(|V|+|E|) a d b cf h e g i proc topsort () for each vertex v do if in[v]=0 then Enqueue(v,Q) while not empty(Q) do Dequeue(v,Q) print(v) for each vertex w in adj[v] do if (--in[w]=0) then Enqueue(w,Q) O(|V|) O(|E|)
7
6 Unweighted shortest path n Find length of the shortest path from a vertex v n Naïve algorithm would take time O(|V| 2 ) n An O(|V|+|E|) algorithm: a d e b cf j h g i procedure shortest-path(v) for each vertex x do visited[x] = false dist[x] = infinity visited[v] = true dist[v] = 0 Enqueue(v,Q) while not Empty(Q) do Dequeue(x,Q) for each w in adj[x] do if not visited[w] visited[w] = true dist[w] = dist[x]+1 Enqueue(w,Q)
8
7 Graph traversal: breadth-first search a b c de a d e b cf j h g i n Complexity: O(N+|E|) for each vertex v do visited[v] = false for each vertex v do if visited[v] = false BFS (v) proc BFS (v) // Q is a queue of vertices visited[v] = true Enqueue(v,Q) while not Empty(Q) do Dequeue(x,Q) for each vertex w in adj[x] do if not visited[w] visited[w] = true Enqueue(w,Q) O(|V|) O(|E|)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.