Algorithm Analysis Graph algorithm Chung-Ang University, Jaesung Lee
Basic definitions Graph = (, ) where is a set of vertices and is a set of edges Directed graph =<, > where consists of ordered pairs Weighted graph with a weight function ( ) where Degree of a vertex : deg The number of incoming edges to : indeg The number of outgoing edges from : outdeg Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 2
Representation of Graphs Considering graph =, where =,,, Adjacency matrix: matrix =, where, =,, 0, h Weighted adjacency matrix:, =,,,, h Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 3
Representation of Graphs Considering graph =, where =,,, Adjacency list: an array,, of pointers 2 3 2 3 2 0 0 2 3 3 0 0 3 2 Adjacency matrix Adjacency list Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 4
Paths, Cycles and Subgraphs Path : a sequence of vertices = (,,, ) where,, Simple path: where, Cycle: a sequence of vertices = (,,,, = ) where,, Simple cycle: where, except the pair, Subgraphs: a graph =, is iff and Connected graph: for every pair of vertices,, there exists =,, Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 5
Trees and Spanning Trees Tree: a connected acyclic graph Spanning tree: a tree that contains all vertices in and is a subgraph of Let be a spanning tree of a graph. Then Any two vertices in are connected by a unique simple path. If any edge is removed from, then becomes disconnected. If we add any edge into, then the new graph will contain a cycle. Number of edges in is. Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 6
Minimum Spanning Trees (MST) Weight of a spanning tree : the sum of weights of all edges in Minimum Spanning Tree: a spanning tree with the smallest possible weight 2 4 3 5 6 Graph Three (of many possible) spanning trees from Weighted Graph MST of 2 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 7
Minimum Spanning Trees (MST) Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 8
Examples of MST Network Design Airline routes Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 9
MST related algorithms Kruskal s algorithm For a graph with vertices, keep on adding the least cost edge until edges added Prim s algorithm By focusing on vertices instead of edges, yielding a simple algorithm (implementation) Dijkstra s algorithm: The Single Source Shortest-Path problem For example, 2, 3, 3 4, 3 4 5 where =, 2, 3, 4, 5 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 0
Dijkstra s Algorithm Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee
Dijkstra s Algorithm Initial node Y v??? v 2 v Initial node Y v v 5 v 4 v 2 v??? v 3 v??? v 4 v??? v 5 v 5 6 v 4 5 3 4 v 3 7 2 v 2 v v 3 v v 5 v 4 v v 5 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 2
Algorithm: Description v 6 4 7 v 5 v 2 3 2 v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 3
Algorithm: Description Starting point v 6 4 7 v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 4
Algorithm: Description Current intersection v 6 4 7 v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 5
Algorithm: Description Current intersection v 6 4 7 v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 6
Algorithm: Description Current intersection v 6 4 7 0 min(,7) min(,4) min(,6) min(,) v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 7
Algorithm: Description Current intersection v 6 4 7 0 7 4 6 v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 8
Algorithm: Description v Current intersection 6 4 7 0 7 4 6 v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 9
Algorithm: Description v Current intersection 6 4 7 0 7 4 6 v 5 v 2 3 2 Destination v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 20
Algorithm: Description v Current intersection 6 4 7 0 7 4 6 v 5 v 2 3 2 Destination v 4 5 v 3 + + + +0 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 2
Algorithm: Description v Current intersection 6 4 7 0 min (+,7) min (+,4) min (+,6) min (+0,) v 5 v 2 3 2 Destination v 4 5 v 3 + + + +0 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 22
Algorithm: Description v 6 4 7 0 7 4 2 v 5 v 2 3 2 2+3 2+ 2+0 Current intersection Destination v 4 5 v 3 2 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 23
Algorithm: Description v 6 4 7 0 min (2+3,7) min (2+,4) min (2,2) v 5 v 2 3 2 2+3 2+ 2+0 Current intersection Destination v 4 5 v 3 2 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 24
Algorithm: Description v 6 4 7 0 5 4 2 v 5 v 2 3 2 2+3 2+ 2+0 Current intersection Destination v 4 5 v 3 2 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 25
Algorithm: Description v 6 4 7 0 5 4 2 v 5 v 2 3 2 5 2 Current intersection Destination v 4 5 v 3 2 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 26
Algorithm: Pseudocode Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 27
Algorithm: Pseudocode Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 28
Algorithm using Priority Queue Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 29
Algorithm: Procedures Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 30
Algorithm: Complexity Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 3
Kruskal s Algorithm Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 32
Kruskal s Algorithm Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 33
Algorithm: Description v v 6 4 7 v 5 v 2 v 5 v 2 3 2 v 4 v 3 v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 34
Algorithm: Description 6 v 4 7 v 5 v v 2 v 5 v 2 3 2 v 4 v 3 v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 35
Algorithm: Description v 5 6 v 3 4 7 2 v 2 v v 5 3 v 2 2 v 4 v 3 v 4 5 v 3 Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 36
Algorithm: Pseudocode Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 37
Algorithm: Complexity log ( log ) Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 38
Algorithm: Example Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 39
Algorithm: Example Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 40
Algorithm: Example Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 4
Algorithm: Example Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 42
Algorithm: Example Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 43
Prim s Algorithm Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 44
Algorithm: Description Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 45
Algorithm: Description Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 46
Algorithm: Description Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 47
Algorithm: Complexity Minimum edge weight data structure Time complexity in total Adjacency Matrix, searching Binary heap and adjacency List + log = ( log ) Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 48
Algorithm: Complexity Algorithm Analysis / Chung-Ang University / Professor Jaesung Lee 49