1.21k likes | 1.23k Views
Learn advanced data structures and graph algorithms, simplify problems, and apply topological sorting to optimize solutions. Understand different graph representations and solve real-world problems effectively using graphs and algorithms. Dive into topological sorting applications like course scheduling and task sequencing. Explore unique solutions and ordering techniques in directed acyclic graphs. Master topological sort methods for efficient problem-solving.
E N D
G64ADSAdvanced Data Structures Graph Algorithms 1
Going from A to B … • What data structure to use? • How to think about the problem? C A B F D E H G 2
Going from A to B … Strip away the irrelevant details … Vertices C A B F D E H G 3
Going from A to B … Strip away the irrelevant details … Edges C A B F D E H G 4
Going from A to B … Strip away the irrelevant details … Weights 4 C 3 A B 3 F 4 4 D E 4 5 6 H G 5
Going from A to B … Strip away the irrelevant details … Now, much simpler … 4 C 3 A B 3 F 4 4 D E 4 5 6 H G 6
Graphs • Are a tool for modeling real world problems. • Allow us to abstract details and focus on the problem. • We can represent our domain as graphs apply algorithms to solve our problem.
4 3 3 4 4 4 5 6 Weighted Graphs
Representation of Graphs • Adjacency matrix • Adjacency list
Representation of Graphs • Adjacency list
Topological Sort • Ordering of vertices in a directed acyclic graph, such that if there is a path from vi to vj, the vj appears after vi in the ordering
Topological Sort • Application • Given a number of tasks, there are often a number of constraints between the tasks: • task A must be completed before task B can start • These tasks together with the constraints form a directed acyclic graph • A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints
Topological Sort • Application • Consider someone getting ready to go out for dinner • He must wear the following: • jacket, shirt, briefs, socks, tie, Blackberry, etc. • There are certain constraints: • the pants really should go on after the briefs, • the Blackberry must be clipped on the belt • socks are put on before shoes
Topological Sort • Application • Getting dress graph
Topological Sort • Application • Getting dress graph • One topological sort is: • briefs, pants, wallet, keys, belt, Blackberry, socks, shoes, shirt, tie, jacket, iPod, watch
Topological Sort • Application • Getting dress graph another possible solution: briefs, socks, pants, shirt, belt, tie, jacket, wallet, keys, Blackberry, iPod, watch, shoes • One topological sort is: • briefs, pants, wallet, keys, belt, Blackberry, socks, shoes, shirt, tie, jacket, iPod, watch Not unique,
Topological Sort • Course prerequisite structure represented in an acyclic graph
Topological Sort • Topological Sort (Relabel): Given a directed acyclic graph (DAG), relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one 6 1 5 8 3 2 7 4 3 5 2 7 8 4 1 6
Topological Sort • Topological Sort (rearrange): Given a directed acyclic graph (DAG), rearrange its vertices on a horizontal line such that all the directed edges points from left to right. 6 1 5 8 3 2 7 4 7 5 6 2 1 4 8 3
web.mit.edu/jorlin/www/15.082/Animations/Topological_Sorting.pptweb.mit.edu/jorlin/www/15.082/Animations/Topological_Sorting.ppt Following slides are taken from
1 2 3 4 5 6 7 8 Node Indegree 2 2 3 2 1 1 0 2 7 0 next LIST Initialization 6 1 “Next” will be the label of nodes in the topological order. Determine the indegree of each node LIST is the set of nodes with indegree of 0. 5 8 3 2 7 4
LIST Select a node from LIST 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 5 8 3 2 7 7 4 1 0 1 next 1 2 3 4 5 6 7 8 Node Indegree 2 2 3 1 2 0 1 1 0 2 7 5
LIST Select a node from LIST 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 8 3 2 7 7 4 1 2 0 1 next 1 2 3 4 5 6 7 8 Node Indegree 2 2 1 3 0 1 2 0 1 1 0 0 2 4 5 7 6
LIST Select a node from LIST 3 6 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 8 3 2 7 7 4 1 3 1 2 0 next 1 2 3 4 5 6 7 8 Node Indegree 1 2 2 1 0 3 2 1 0 0 1 0 1 0 2 4 7 5 6 2
LIST Select a node from LIST 3 6 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 1 4 1 3 2 0 next 1 2 3 4 5 6 7 8 Node Indegree 0 1 2 1 0 2 3 0 1 2 1 0 1 0 0 2 4 5 7 2 6 1
LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 1 1 2 5 0 4 3 next 1 2 3 4 5 6 7 8 Node Indegree 2 1 0 2 0 1 2 3 2 1 0 0 1 0 1 0 2 1 4 7 5 1 2 6
LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 4 1 6 1 0 2 4 5 3 6 next 1 2 3 4 5 6 7 8 Node Indegree 1 0 2 0 1 2 3 2 1 2 0 1 0 1 1 0 0 1 0 2 6 4 1 2 5 7 8
LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 7 2 5 5 2 2 8 8 3 4 7 7 4 4 1 6 1 4 5 6 2 3 7 0 next 1 2 3 4 5 6 7 8 Node Indegree 2 1 0 0 1 2 0 3 2 1 0 1 2 0 1 0 1 0 2 1 0 8 3
LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 7 8 2 5 5 2 2 8 8 3 3 4 7 7 4 4 1 6 4 1 6 7 8 5 3 2 0 next List is empty. The algorithm terminates with a topological order of the nodes 1 2 3 4 5 6 7 8 Node Indegree 2 0 1 0 1 2 0 2 3 1 2 0 1 0 1 0 1 0 2 0 1 3
Example • Consider the following DAG with six vertices
Example • Let us define the table of in-degrees
Example • And a queue into which we can insert vertices 1 and 6 Queue
Example • We dequeue the head (1), decrement the in-degree of all adjacent vertices, and enqueue 2 Queue Sort 1
Example • We dequeue 6 and decrement the in-degree of all adjacent vertices Queue Sort 1, 6
Example • We dequeue 2, decrement, and enqueue vertex 5 Queue Sort 1, 6, 2
Example • We dequeue 5, decrement, and enqueue vertex 3 Queue Sort 1, 6, 2, 5
Example • We dequeue 3, decrement 4, and add 4 to the queue Queue Sort 1, 6, 2, 5, 3
Example • We dequeue 4, there are no adjacent vertices to decrement the in degree Queue Sort 1, 6, 2, 5, 3, 4
Example • The queue is now empty, so a topological sort is 1, 6, 2, 5, 3, 4
Shortest-Path Algorithm • Find the “shortest” path from point A to point B • “Shortest” in time, distance, cost, … • Numerous applications • Map navigation • Flight itineraries • Circuit wiring • Network routing
Shortest Path Problems • Input is a weighted graph where each edge (vi,vj) has cost ci,j to traverse the edge • Cost of a path v1v2…vN • Unweighted path length is N-1, number of edges on path
Shortest Path Problems • Single-source shortest path problem • Given a weighted graph G=(V,E), and a start vertex s, find the minimum weighted path from s to every other vertex in G
Negative Weights • Graphs can have negative weights • e.g., arbitrage • Shortest positive-weight path is a net gain • Path may include individual losses • Problem: Negative weight cycles • Allow arbitrarily-low path costs • Solution • Detect presence of negative-weight cycles
Shortest Path Problems • Unweighted shortest-path problem: O(|E|+|V|) • Weighted shortest-path problem • No negative edges: O(|E| log |V|) • Negative edges: O(|E|·|V|) • Acyclic graphs: O(|E|+|V|) • No asymptotically faster algorithm for single-source/single-destination shortest path problem
Unweighted Shortest Paths • No weights on edges • Find shortest length paths • Same as weighted shortest path with all weights equal • Breadth-first search