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

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Traffic assignment.
Single Source Shortest Paths
ALG0183 Algorithms & Data Structures
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Chen, Lu Mentor: Zhou, Jian Shanghai University, School of Management Chen213.shu.edu.cn.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Shortest Paths Definitions Single Source Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
Algorithms All pairs shortest path
All-Pairs Shortest Paths
Chapter 4 Graphs.
Routing Protocols and the IP Layer CS244A Review Session 2/01/08 Ben Nham Derived from slides by: Paul Tarjan Martin Casado Ari Greenberg.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Graphs, BFS, DFS and More…
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Lecture 16. Shortest Path Algorithms
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Shortest Path Graph Theory Basics Anil Kishore.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Basic Graph Algorithms Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 14, 2014.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Paths and Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Unit 3 Graphs.
Network Routing.
Graph Algorithm.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Graph Operations And Representation
Autumn 2015 Lecture 10 Minimum Spanning Trees
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Case study: Strava + Waze
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
離散數學 DISCRETE and COMBINATORIAL MATHEMATICS
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Chapter 24: Single-Source Shortest Paths
Text Book: Introduction to algorithms By C L R S
CE 221 Data Structures and Algorithms
Chapter 24: Single-Source Shortest Paths
Dijkstra's Shortest Path Algorithm
CE 221 Data Structures and Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Graphs: Shortest path and mst
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Graph Algorithm.
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

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 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.*/ 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; 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"

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.

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

Thank you for your attention DISCRETE MATHEMATICS Czesław Smutnicki