Graphs As a mathematics branch Map of Knigsberg
Graphs: As a mathematics branch Map of Königsberg 1 2 3 4 Leonhard Paul Euler 1707 -1783 Can we cross every bridge exactly once and return to the same place? (There is no other way but through the bridges to cross the rivers) Euler’s solution is considered as the first Graph theorem in history. 2/24/2021 IT 279 1
Eulerian Graph 1 2 4 Map of Königsberg 3 Starting from a vertex, can we travel every edges exactly once and return to the same vertex? Leonhard Paul Euler 1707 -1783 If we can, such a path is called an Eulerian Circuit No! Theorem [Euler 1737]: A graph has an Eulerian Circuit iff the degree of every vertex is even. The first Graph theorem in history. 2/24/2021 IT 279 2
More Terminologies (v 1, v 2, . . vn) �Vn , such that (vi , vi+1) �E, for 1 �i < n, is called a walk of G= (V, E) 1. A path is a walk such that no vertex in the walk is repeated. 2. A trail is a walk such that no edge in the walk is repeated. (Any path is a trial) 3. A circuit is a closed trial, i. e, v 1 = vn. 4. A cycle is a circuit such that v 1 = vn is only repeated vertex. 5. A spanning is a walk such that every vertex is included 6. A Eulerian circuit is a circuit that contains every edge. 7. A Hamiltonian cycle is a spanning cycle. 2/24/2021 IT 279 3
Topological Sort (topological order) Let G = (V, E) be a directed graph with |V| = n. A Topological Sort is a sequence v 1, v 2, . . vn, such that 1. { v 1, v 2, . . vn } = V, and 2. for every i and j with 1 i < j n, (vj , vi) E. g a f h e c b d h g e b d c 2/24/2021 a There are more than one such order in this graph!! f IT 328, review graph algorithms 4
Algorithm for finding Topological Sort x Principle: vertex with in-degree 0 should be listed first. x y Algorithm: z y a b Input G=(V, E) Repeat the following steps until no more vertex left: 1. Find a vertex v with in-degree 0; If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic 2. Remove v from V and update E 3. Add v to the end of the sort 2/24/2021 IT 328, review graph algorithms 5
Topological Sort (topological order) of an acyclic graph g a f h e c b d h g e b d c 2/24/2021 a f IT 328, review graph algorithms 6
Finding the shortest path between two vertices Starting Vertex Unweighted Graphs Weighted Graphs 0 0 2 1 7 Final Vertex 2/24/2021 Both are using the greedy algorithm. 7 1 9 2 35 5 3 2 2 1 1 5 12 8 10 w w IT 328, review graph algorithms Final Vertex 7
Construct a shortest path Map for weighted graph Starting Vertex 0, -1 0 2 5 1 1 1 1 9 5, 0 3, 1 7 4 2, 0 2 5 3 : decided 7, 1 10, 2 8, 4 5 w-1, y n 2/24/2021 Final Vertex IT 328, review graph algorithms w, x 8
A spanning is a walk such that every vertex is included Minimum Spanning Tree of an undirected graph: The lowest cost to connect all vertices Since a cycle is not necessary, such a connection must be a tree. 2 1 1 4 2 3 6 2/24/2021 10 3 7 4 8 5 2 5 4 1 IT 328, review graph algorithms 6 7 9
Prim’s algorithm: grow the minimum spanning tree from a root 2 1 4 2 3 7 4 8 5 10 3 5 4 1 6 7 2 1 6 2 1 2 3 4 5 4 6 2/24/2021 1 IT 328, review graph algorithms 6 7 10
Prim’s algorithm: grow the minimum spanning tree from a different root 2 1 4 2 3 7 4 8 5 10 3 5 4 1 6 7 2 1 6 2 1 2 3 4 5 4 6 2/24/2021 1 IT 328, review graph algorithms 6 7 11
Kruskal’s Algorithm: construct the minimum spanning from edges 2 1 4 2 3 7 4 8 5 10 3 5 4 1 6 7 2 1 6 2 1 2 3 4 5 4 6 2/24/2021 1 IT 328, review graph algorithms 6 7 12
Maximum Flow Problem flow capacity Source s actual flow 3/ 4 2/ 2 1/ 1 a b 2/3 d 2/ 2 1/2 c e 1. Actual flow capacity 2. The source node can produce flow as much as possible 3. For every node other than s and t, flow out = flow in The target node’s flow-in is called the flow of the graph f 1/ 2 2/24/2021 Target (sink) A node may have different ways to dispatch its flow-out. g 2/ 3 t What is the maximum flow of the graph? IT 279 13
What makes the maximum flow problem more difficult than the shortest path problem? Source s 3/ 4 2/ 2 1/ 1 a d f 2/ 2 1/2 c e The maximum flow to t is not based on the maximum flows to f and g. g 1/ 2 Target once a node’s shortest path from s is determined, we will never have to revise the decision b 2/3 2/24/2021 The shortest path of t is based on the shortest path of g. 2/ 3 t IT 279 14
local optimum does not imply global optimum Source s s 4/5 3/3 1/4 3/3 c 5/5 2/2 0/1 a Source b 2/3 d a 0/3 c s 2/2 1/1 5/5 b 4/4 3/3 0/3 d a 1/3 c 2/2 1/1 3/4 1/3 b 3/3 d Target s c max flow = 6 2/24/2021 2/2 s d max flow = 7 IT 279 Target 5/5 t s t max flow = 7 15
A naïve approach: adding all possible paths together Source s s 2/3 a 1 2 c d 2/ 2 3 Target t a b 4 2/ 3 Source s 2/ 2 1 4 1 1/1 b 2/ 2 Target t IT 279 b 1 d c 2/3 Residual Graph 2/24/2021 1 1/ 4 d c a 1/1 Target t Residual Graph 16
Source s s 3/3 a 1 b a 3 1 d Target t b 2/2 c d 2/ 2 3/3 Target t Not always work. We are just lucky Residual Graph 2/24/2021 1 1/4 3 c 2/2 IT 279 17
A wrong choice: Source 3/3 Source s 2 1 a 2 b 3/4 3 2 Target 1 a 2 2 d c 3/3 b 1 3 d c 2/24/2021 s 2 t Target IT 279 t 18
Keep a path for changing decision: Source 3/3 Source s 2 1 a s b 3/4 3 2 Target 1 a 2 b 1 3 2 3 d c 3/3 2 t Target 3 t Augmented Graph 2/24/2021 IT 279 19
Find another path in the augmented graph No more path from s to t Source s s 2/2 3 a 1 2/3 d 2/ 2 Target 3 2 3 b a 2 1 2 1 c 3 b 3 1 2 d 2 t Augmented Graph 2/24/2021 1 a 2/2 2/3 c 3 b 1 s 2 1 c d 2 t 3 t new Augmented Graph IT 279 20
Add flows together Source 3 + s = s 3 2 a b 3 a b 2 2 d b 2 1 d c 3 2 t 2 d c 3 Target 2 a 2 c s 2 Target t Maximum Flow 2/24/2021 IT 279 21
An algorithm for finding the maximum flow of graphs Input: G = (V, E), and s, t V Let Gmax = ( , ), Gaug = G 1. Find a path p from s to t in Gaug 2. If no such p exists, output Gmax and stop the program 3. Add p into Gmax and update Gaug 4. Repeat 1, 2, and, if possible, 3 Complexity: 2/24/2021 O(c |E|) IT 279 22
A Eulerian cycle (path) is a trail that goes through every edge exactly once. b c a h i j g n k f m e o l d p 2/24/2021 IT 328, review graph algorithms 23
A Hamiltonian cycle (path) is a cycle that contains every vertex; A graph that contains a Hamiltonian cycle is called Hamiltonian b c a h i j g n k f m e o l d p 2/24/2021 IT 328, review graph algorithms 24
Back Tracking Going through a Maze 2/24/2021 Try this java jar IT 328 25
- Slides: 25