100 likes | 254 Views
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;
E N D
Internet Engineering Czesław Smutnicki DiscreteMathematics– 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
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
SHORTEST PATHS IN Graph FROM source. DIJKSTRA’S ALGORITHM functionDijkstra(Graph, source): for eachvertexv in Graph: // Initializations dist[v] := infinity ; // Unknowndistancefunction from source to v previous[v] := undefined ; // Previousnode in optimalpath from source end for ; dist[source] := 0 ; // Distance from source to source Q:= the set of allnodes in Graph ; // All nodes in the graphareunoptimized - thusare in Q whileQis notempty: // The mainloop u:= vertex in Q with smallestdist[] ; ifdist[u] = infinity: break; // allremainingverticesareinaccessible from source fi; removeu from Q ; for eachneighborv of u: // where v has not yetbeenremoved from Q. alt:= dist[u] + dist_between(u, v) ; ifalt < dist[v]: // Relax (u,v,a) dist[v] := alt ; previous[v] := u ; fi ; end for ; end while ; returndist[] ; endDijkstra.
SHORTEST PATHS BETWEEN EACH PAIR OF NODES. FLOYD-WARSHALL ALGORITHM /* Assume a functionedgeCost(i,j) whichreturns the cost of the edge from i to j (infinityifthereisnone). Alsoassumethatnis the number of vertices and edgeCost(i,i) = 0 */ intpath[][]; /* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortestpath from i to j usingintermediatevertices (1..k−1). Eachpath[i][j] isinitialized to edgeCost(i,j). */ procedureFloydWarshall () for k := 1 to n for i := 1 to n for j := 1 to n path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );
SHORTEST PATHS BETWEEN EACH PAIR OF NODES. FLOYD-WARSHALL ALGORITHM cont. procedureFloydWarshallWithPathReconstruction () for k := 1 to n for i := 1 to n for j := 1 to n ifpath[i][k] + path[k][j] < path[i][j] then path[i][j] := path[i][k]+path[k][j]; next[i][j] := k; procedureGetPath (i,j) ifpath[i][j] equalsinfinitythen return "no path"; intintermediate := next[i][j]; ifintermediateequals 'null' then return " "; /* thereisanedge from i to j, with no verticesbetween */ else returnGetPath(i,intermediate) + intermediate + GetPath(intermediate,j);
SHORTEST PATHS FROM source. BELLMAN-FORD ALGORITHM procedureBellmanFord(listvertices, listedges, vertexsource) // Thisimplementationtakesin a graph, represented as lists of vertices // and edges, and modifiesthevertices so thattheirdistance and //predecessorattributesstoretheshortestpaths. // Step 1: initializegraph for eachvertex v invertices: if v issourcethenv.distance := 0 elsev.distance := infinity v.predecessor := null // Step 2: relaxedgesrepeatedly for i from 1 tosize(vertices)-1: for eachedgeuvinedges: // uvistheedgefrom u to v u := uv.source v := uv.destination ifu.distance + uv.weight < v.distance: v.distance := u.distance + uv.weight v.predecessor := u // Step 3: check for negative-weightcycles for eachedgeuvinedges: u := uv.source v := uv.destination ifu.distance + uv.weight < v.distance: error "Graphcontains a negative-weightcycle"
MAXIUMUM FLOW FROM source TO sink. FORD-FULKERSON ALGORITHM class Edge: def __init__(self, u, v, w): self.source = u self.sink = v self.capacity = w def __repr__(self): return str(self.source) + "->" + str(self.sink) + " : " + str(self.capacity) classFlowNetwork(object): def__init__(self): self.adj, self.flow, = {}, {} defadd_vertex(self, vertex): self.adj[vertex] = [] defget_edges(self, v): return self.adj[v] defadd_edge(self, u, v, w=0): assert(u != v) edge = Edge(u,v,w) redge = Edge(v,u,0) edge.redge = redge redge.redge = edge self.adj[u].append(edge) self.adj[v].append(redge) self.flow[edge] = 0 self.flow[redge] = 0
MAXIUMUM FLOW FROM source TO sink. FORD-FULKERSON ALGORITHM. cont deffind_path(self, source, sink, path): ifsource == sink: returnpath foredge in self.get_edges(source): residual = edge.capacity - self.flow[edge] ifresidual > 0 and not (edge,residual) inpath: result = self.find_path( edge.sink, sink, path + [(edge,residual)] ) ifresult != None: returnresult defmax_flow(self, source, sink): path = self.find_path(source, sink, []) whilepath != None: flow = min(res for edge,res in path) foredge,resinpath: self.flow[edge] += flow self.flow[edge.redge] -= flow path = self.find_path(source, sink, []) return sum(self.flow[edge] for edge in self.get_edges(source))
Thank you for your attention DISCRETE MATHEMATICS Czesław Smutnicki