Содержание
- 2. Shortest paths and spanning trees in graphs Lyzhin Ivan, 2015
- 3. Shortest path problem The problem of finding a path between two vertices such that the sum
- 4. Dijkstra algorithm There are two sets of vertices – visited and unvisited. For visited vertices we
- 5. Trivial implementation void dijkstra(int s) { vector mark(n, false); vector d(n, INF); d[s] = 0; for
- 6. Implementation with set void dijkstra(int s) { set > q; //(dist[u], u) vector dist(n, INF); dist[s]
- 7. Implementation with priority queue void dijkstra(int s) { priority_queue > q; //(dist[u], u) vector dist(n, INF);
- 8. Floyd–Warshall algorithm Initially, dist[u][u]=0 and for each edge (u, v): dist[u][v]=weight(u, v) On iteration k we
- 9. Implementation void floyd_warshall() { vector > dist(n, vector (n, INF)); for (int i = 0; i
- 10. Bellman–Ford algorithm |V|-1 iterations, on each we try relax distance with all edges. If we can
- 11. Implementation void bellman_ford(int s) { vector dist(n, INF); dist[s] = 0; for (int i = 0;
- 12. Minimal spanning tree A spanning tree T of an undirected graph G is a subgraph that
- 13. Prim’s algorithm Initialize a tree with a single vertex, chosen arbitrarily from the graph. Grow the
- 14. Implementation void prima() { set > q; //(dist[u], u) vector dist(n, INF); dist[0] = 0; q.insert(mp(0,
- 15. Kruskal’s algorithm Create a forest F (a set of trees), where each vertex in the graph
- 16. Trivial implementation void trivial_kruskal() { vector color(n); for (int i = 0; i color[i] = i;
- 17. Implementation with DSU void kruskal() { DSU dsu(n); sort(edges.begin(), edges.end()); for(auto e : edges) if(dsu.findSet(e.u)!=dsu.findSet(e.v)) {
- 18. Disjoint-set-union (DSU) Two main operations: Find(U) – return root of set, which contains U, complexity O(1)
- 19. Implementation struct DSU { vector p; DSU(int n) { p.resize(n); for (int i = 0; i
- 20. Path compression When we go up, we can remember root of set for each vertex in
- 21. Union by size int unionSets(int u, int v) { int pu = findSet(u); int pv =
- 22. Links https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm https://en.wikipedia.org/wiki/Bellman–Ford_algorithm https://en.wikipedia.org/wiki/Kruskal%27s_algorithm https://en.wikipedia.org/wiki/Prim%27s_algorithm https://en.wikipedia.org/wiki/Disjoint-set_data_structure http://e-maxx.ru/algo/topological_sort
- 24. Скачать презентацию