Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms.

Similar presentations


Presentation on theme: "Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms."— Presentation transcript:

1 Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms

2 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

3 GRAPHS. BASIC NOTIONS nodes arcs, edges weights graph, digraph, multigraph node degree, isolated node, leaf tree, spanning tree, rooted/unrooted tree path, path length, chain cycle, Hamiltonian cycle, cycle length AoN, AoA representations flow, node divergence, cut, minimal cut data structures for graphs complexity analysis

4 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.

5 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] );

6 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);

7 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"

8 MAXIUMUM FLOW FROM source TO sink. FORD-FULKERSON ALGORITHM 1.class Edge: 2.def __init__(self, u, v, w): 3.self.source = u 4.self.sink = v 5.self.capacity = w 6.def __repr__(self): 7.return str(self.source) + "->" + str(self.sink) + " : " + str(self.capacity) 8.class FlowNetwork(object): 9.def __init__(self): 10.self.adj, self.flow, = {}, {} 11.def add_vertex(self, vertex): 12.self.adj[vertex] = [] 13.def get_edges(self, v): 14.return self.adj[v] 15.def add_edge(self, u, v, w=0): 16.assert(u != v) 17.edge = Edge(u,v,w) 18.redge = Edge(v,u,0) 19.edge.redge = redge 20.redge.redge = edge 21.self.adj[u].append(edge) 22.self.adj[v].append(redge) 23.self.flow[edge] = 0 24.self.flow[redge] = 0 25.

9 MAXIUMUM FLOW FROM source TO sink. FORD-FULKERSON ALGORITHM. cont 26.def find_path(self, source, sink, path): 27.if source == sink: 28.return path 29.for edge in self.get_edges(source): 30.residual = edge.capacity - self.flow[edge] 31.if residual > 0 and not (edge,residual) in path: 32.result = self.find_path( edge.sink, sink, path + [(edge,residual)] ) 33.if result != None: 34.return result 35.def max_flow(self, source, sink): 36.path = self.find_path(source, sink, []) 37.while path != None: 38.flow = min(res for edge,res in path) 39.for edge,res in path: 40.self.flow[edge] += flow 41.self.flow[edge.redge] -= flow 42.path = self.find_path(source, sink, []) 43.return sum(self.flow[edge] for edge in self.get_edges(source))

10 Thank you for your attention DISCRETE MATHEMATICS Czesław Smutnicki


Download ppt "Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms."

Similar presentations


Ads by Google