Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Topological Sorting (Sequencing vertices)
- Only applicable to directed acyclic graphs (DAG)
- Vertices are tasks
- edges specify precedence
- x -> y -> y cannot be done until x is finished
- i.e. precedence graph
- Find a sequence in which tasks may be performed while obeying all precedence constraints
- Assign topological numbers to vertices if topnum(x) = i - > i is in position of x in topological sorted sequence i is 0... n-1
- Precedence: if x -> y, then topnum(x) < topnum(y)
- _________
- | |
- A - > B - > D - > G - > I
- | 2 ^3 ^6 ^7
- | \ / | / / Possible Sequence A C B D E H G I
- | > C - > E - > H
- | 1 4 ^5
- |___________________|
- BFS top sort
- compute indegree of each vertex O(n+e)
- BFS based sort
- 1. for each vertex with indegree O, number that verex with next highest number (0, 1, 2 ... ) enqueue O(n)
- 2. while queue is not empty do O(n+e)
- V <- dequeue()
- for each number w of V do
- indegree[w]--;
- if (indegree[w] == 0
- assign next number to w
- number++; O(n)
- enqueue(v);
- endif
- endfor
- endwhile
- O(n) + O(n+e) + O(n) + O(n+e)
- Dijkstra's Shortest(single source) Paths Algorithm (Any weighted undirected/directed)
- Q. Shortest path from A to F
- Trial & Error Approach
- A -5-> B -6-> D -6-> F
- | \ ^ ^
- 10 3 2 2
- | \ / |
- C<---2----E----2---->G
- Distance Array D -> keeps current shortest distance of vertex from A (in implementation, would be largest number storable i.e. Integer.Max.Value
- [0 5 10 11 8 inf inf]
- A B C D E F G
- Previous Array -> contents previous array are vertex numbers in implementation
- [A A A E B G E]
- Step|Done|D[B]|D[C]|D[D}|D[E]|D[F]|D[G]|
- ----------------------------------------
- 0 | A |5,A |10,A|inf |inf |inf |inf |
- ----------------------------------------
- 1 | B | |10,A|11,B|8,B | |inf |
- -----------------------------------
- 2 | E | |10,A|10,E| | |10,E| <- For next pick, any vertex u ok -> tie broken arbitrarily
- ----------------------------------------
- 3 | C | | |10,E| | |10,E|
- ----------------------------------------
- 4 | D | | | | |16,D|10,E|
- ----------------------------------------
- 5 | G | | | | |12,G| |
- ----------------------------------------
- Fringe -> all vertices that are not done whose distance is not infinity
- Done -> distance will never be lower at any later point
- Fringe : B(5), C(10)
- Loop:
- Take Minimum distance out of fringe, this vertex is done
- For each neighbor, compute new distance and update with new distance if less than old.
- Use Stack to follow breadcrumb trail of previous vertices
- Single Source
- The first time a question is asked from a starting point X, run the algo through all vertices (dont stop when X is done)
- stopping only when fringe is empty. This will compute shortest paths to all vertices
- Store all the questions with same source X, just look up path in stored file/db
- Induced Shortest Path Tree(SPT)
- When Algo is run all the way, store this:
- A
- 5 / \ 10
- 5 B C 10
- 3 |
- 8 C
- 2 | \ 2
- 10 D G 10
- | 2
- F 12
- Worst Case Running Time Analysis
- Algorithmic Components
- - Add Vertex to Fringe
- - Pick (delete) minimum distance vertex from fringe
- - Check now distance if neighbor against old
- - Update distance of neighbor, if new distance is less than old
- Graphs stored in adjacency linked lists
- Fringe Version 1: Unsorted linked list (when adding to fringe, add to front of LL)
- Worse Case
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement