Skip to main content

Posts

Showing posts from June, 2023
Shortest path in weighted path Shortest path in weighted path weighted graph G = ( V , E ) G = (V, E) G = ( V , E ) with w ( u , v ) w(u,v) w ( u , v ) the weight of edge ( u , v ) (u,v) ( u , v ) . shortest path problem: what is the shortest path between u and v in a weighted graph? - the cost of a path is the sum of the weights of the edges on the path - the shortest path between u and v is the path with the smallest cost The distance d ( u , v ) d(u,v) d ( u , v ) is the length of the shortest path between u and v. A sub-path of a shortest path is a shortest path. Single-source shortest paths what is the shortest path from one vertex to all other vertices? Example: what's the shortest path from my home to school? Weights can be related to time, money, etc. Network routing: what's the shortest path from a source computer to all other computers in the network? Di...

Recap on BFS, DFS and Strongly Connected Components

graph graph DFS topological sort considering the dependencies between tasks, we can use topological sort to find the order of tasks. def topological_sort ( graph ): visited = set () stack = [] for node in graph: if node not in visited: dfs(graph, node, visited, stack) return stack[::- 1 ] def dfs ( graph, node, visited, stack ): visited.add(node) for neighbor in graph[node]: if neighbor not in visited: dfs(graph, neighbor, visited, stack) stack.append(node) if A -> B then B.finishtime < A.finishtime, that's why we need to reverse the stack. Topological Sorting on a DAG do DFS on the graph when a node is finished, insert it into the front of a linked list Another use of DFS is in-order traversal of a binary tree . BFS BFS is used to find the shortest path in an unweighted graph . we can...