Presentation on theme: "Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms."— Presentation transcript:
Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms
CONTENTS Fundamental notions Shortest path Shortest path from the source; non- negative weights; Dijkstra’s algorithm; Shortest path between each pair of nodes; Floyd-Warshall algorithm; Shortest path from the source; any weights; Bellman-Ford algorithm; Longest path Longest path in acyclic graph; Bellman algorithm; Longest path in any graph; Bellman- Ford algorithm; Minimal spanning tree Prime algorithm Kruskal algorithm Maximum flow Definitions and properties Ford-Fulkerson algorithm Dinic algorithm
SHORTEST PATHS IN Graph FROM source. DIJKSTRA’S ALGORITHM 1.function Dijkstra(Graph, source): 2.for each vertex v in Graph: // Initializations 3.dist[v] := infinity ; // Unknown distance function from source to v 4.previous[v] := undefined ; // Previous node in optimal path from source 5.end for ; 6.dist[source] := 0 ; // Distance from source to source 7.Q := the set of all nodes in Graph ; // All nodes in the graph are unoptimized - thus are in Q 8.while Q is not empty: // The main loop 9.u := vertex in Q with smallest dist ; 10.if dist[u] = infinity: 11.break ; // all remaining vertices are inaccessible from source 12.fi ; 13.remove u from Q ; 14.for each neighbor v of u: // where v has not yet been removed from Q. 15.alt := dist[u] + dist_between(u, v) ; 16.if alt < dist[v]: // Relax (u,v,a) 17.dist[v] := alt ; 18.previous[v] := u ; 19.fi ; 20.end for ; 21.end while ; 22.return dist ; 23.end Dijkstra.
SHORTEST PATHS BETWEEN EACH PAIR OF NODES. FLOYD-WARSHALL ALGORITHM 1./* Assume a function edgeCost(i,j) which returns the cost of the edge from i to j 2.(infinity if there is none). 3.Also assume that n is the number of vertices and edgeCost(i,i) = 0 4.*/ 5. 6.int path; 7./* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path 8.from i to j using intermediate vertices (1..k−1). Each path[i][j] is initialized to 9.edgeCost(i,j). 10.*/ 11. 12.procedure FloydWarshall () 13.for k := 1 to n 14.for i := 1 to n 15.for j := 1 to n 16.path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );
SHORTEST PATHS BETWEEN EACH PAIR OF NODES. FLOYD-WARSHALL ALGORITHM cont. 17.procedure FloydWarshallWithPathReconstruction () 18.for k := 1 to n 19.for i := 1 to n 20.for j := 1 to n 21.if path[i][k] + path[k][j] < path[i][j] then 22.path[i][j] := path[i][k]+path[k][j]; 23.next[i][j] := k; 24. 25.procedure GetPath (i,j) 26.if path[i][j] equals infinity then 27.return "no path"; 28.int intermediate := next[i][j]; 29.if intermediate equals 'null' then 30.return " "; /* there is an edge from i to j, with no vertices between */ 31.else 32.return GetPath(i,intermediate) + intermediate + GetPath(intermediate,j);
SHORTEST PATHS FROM source. BELLMAN-FORD ALGORITHM 1.procedure BellmanFord(list vertices, list edges, vertex source) 2.// This implementation takes in a graph, represented as lists of vertices 3.// and edges, and modifies the vertices so that their distance and 4.// predecessor attributes store the shortest paths. 5.// Step 1: initialize graph 6.for each vertex v in vertices: 7.if v is source then v.distance := 0 8.else v.distance := infinity 9.v.predecessor := null 10.// Step 2: relax edges repeatedly 11.for i from 1 to size(vertices)-1: 12.for each edge uv in edges: // uv is the edge from u to v 13.u := uv.source 14.v := uv.destination 15.if u.distance + uv.weight < v.distance: 16.v.distance := u.distance + uv.weight 17.v.predecessor := u 18.// Step 3: check for negative-weight cycles 19.for each edge uv in edges: 20.u := uv.source 21.v := uv.destination 22.if u.distance + uv.weight < v.distance: 23.error "Graph contains a negative-weight cycle"