Download presentation
Presentation is loading. Please wait.
1
Graphs: As a mathematics branch
Map of Königsberg 1 2 3 4 Leonhard Paul Euler 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. 11/10/2018 ACM-ICPC
2
Eulerian Graph 1 Map of Königsberg 2 3 4 Starting from a vertex, can we travel every edges exactly once and return to the same vertex? Leonhard Paul Euler No! If we can, such a path is called an Eulerian Circuit Theorem [Euler 1737]: A graph has an Eulerian Circuit iff the degree of every vertex is even. The first Graph theorem in history. 11/10/2018 ACM-ICPC
3
Graphs: Algorithmic approach
Many problems are unsolvable (usually are associated to infinite graphs) ; Many problems are intractable (NPC problems), And yet, many applicative problems are manageable; (in terms of |V| and |E|) G = (V, E) V: a set of vertices E: a set of edges, E V V 1 Example, V: = {1,2,3,4,5,6} E: = {(1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6)} 2 5 6 3 4 11/10/2018 ACM-ICPC
4
Undirected Graphs G= (V,E) is an undirected graph, if (v, w) E (w, v) E Example, V: = {1,2,3,4,5,6} E: = {(1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6) (2,1), (5,1), (3,2), (5,2), (4,3), (5,4), (6,4) } 1 V: = {1,2,3,4,5,6} E: = { {1,2}, {1,5}, {2,3}, {2,5}, {3,4}, {4,5}, {4,6} } 2 5 6 3 4 11/10/2018 ACM-ICPC
5
Weighted Graphs Every edge in E has a value associated to it. Example,
1 Example, 2.3 3 V: = {1,2,3,4,5,6} E: = {(1,2,3), (1,5,2.3), (2,3,4.5), (2,5,2/3), (3,4,7.1), (4,5,10), (4,6,12)} 2 2/3 5 6 4.5 10 12 3 7.1 4 Weighted graph: G= (V,E), where E V V Q rational numbers 11/10/2018 ACM-ICPC
6
A walk on a graph (v1, v2,.... vn) Vn , such that (vi , vi+1) E, for 1 i < n, is called a walk of G= (V,E) Example: (1,2,4,7,9,8,5,4,7,6,5,2,3) a walk 2 4 7 9 1 3 5 8 6 11/10/2018 ACM-ICPC
7
More Terminologies (v1, v2,.... vn) Vn , such that (vi , vi+1) E, for 1 i < n, is called a walk of G= (V,E) A path is a walk such that no vertex in the walk is repeated. A trail is a walk such that no edge in the walk is repeated. (Any path is a trial) A circuit is a closed trial, i.e, v1 = vn. A cycle is a circuit such that v1 = vn is only repeated vertex. A spanning is a walk such that every vertex is included A Eulerian circuit is a circuit that contains every edge. A Hamiltonian cycle is a spanning cycle. 11/10/2018 ACM-ICPC
8
A Walk: 1,2,4,7,9,8,5,4,7,6,5,2,3 (not a trial) 2 4 7 9 1 3 5 8 6 11/10/2018 ACM-ICPC
9
A Circuit: 1,2,4,7,9,8,5,4,7,6,5,2,3,1 (not a cycle)
A Trial: 1,2,4,7,9,8,5,4,0,7,6,5,2,3 (not a path) A Circuit: 1,2,4,7,9,8,5,4,7,6,5,2,3,1 (not a cycle) 2 4 7 9 1 3 5 8 6 11/10/2018 ACM-ICPC
10
A Path: 1,2,4,0,7,6,5,3 A Cycle: 1,2,4,0,7,6,5,3,1 There are many other cycles in this graph 2 4 7 9 1 3 5 8 6 11/10/2018 ACM-ICPC
11
A graph without any cycle in it is called acyclic
2 4 7 9 1 3 5 8 6 An acyclic Graph 11/10/2018 ACM-ICPC
12
Representation of Graphs. (Data Structures)
Adjacency Matrix Good for dense graph. 1 2 3 4 5 6 7 8 9 2 4 7 9 1 3 5 8 6 11/10/2018 ACM-ICPC
13
Representation of Graphs.
Adjacency Matrix for a Weighted Graph 1 2 3 4 5 2.3 4.5 2/3 7.1 10 12 2.3 3 1 2/3 4 5 4.5 10 12 2 7.1 3 11/10/2018 ACM-ICPC
14
Representation of Graphs. (Data Structures)
Adjacency List A commonly used representation 2 4 7 1 3 5 6 9 8 2 4 7 9 1 3 5 8 6 11/10/2018 ACM-ICPC
15
Representation of Graphs.
Adjacency List for a Weighted Graph (1, 3) (4, 2.3) 1 (2, 4.5) (4, 2/3) 2 (3, 7.1) 3 (4, 10) (5, 12) 4 5 2.3 3 1 2/3 4 5 4.5 10 12 2 7.1 3 How to implement? 11/10/2018 ACM-ICPC
16
Representation of Graphs. (Data Structures)
Adjacency List Should be easy to insert/delete vertices and edges Should be easy to retrieve. Should not use to much memory. 2 4 7 1 2 3 4 5 6 7 8 9 1 3 4 5 1 7 3 4 6 vector deque stack set map 1 6 9 5 9 array (dynamic) linked lists 11/10/2018 ACM-ICPC
17
<set> Red-Black Tree 10 6 12 3 8 15 5 9
#include <iostream> #include <set> set<int> S; while (....) { int e; // an element of S S.insert(e); } set<int>::iterator itr; itr = S.begin(); S.erase(itr); for (itr= S.begin(); itr != S.end(); itr++) cout << *itr; 6 12 3 8 15 5 9 11/10/2018 ACM-ICPC
18
<map> Red-Black Tree key, associated value in, double
10, 1.2 Red-Black Tree #include <iostream> #include <map> map<int, double> M; int i; double w; while (....) { // e is an element of M pair e = make_pair(i,w); M.insert(e); } map<int, double>::iterator itr; itr = M.begin(); M.erase(itr); for (itr= M.begin(); itr != M.end(); itr++) cout << “\n(“ << itr->first << “,” << itr->second << “)”; 6, 3.4 12, 1.2 3, 3.4 8, 5.6 15, 0.3 5, 2.8 9, 0.1 key, associated value in, double 11/10/2018 ACM-ICPC
19
Using STL to implement Graphs
typedef map<int, double> AdjacencyList; typedef map<int, AdjacencyList> Graph; 3 2.3 Graph G; 0.6 3, 1 4 5 4.5 6.3 10 12 1, 4, G[4] 2 3 7.1 G[5] 0, 2, 5, (1, 3) (4, 2.3) 1 (2, 4.5) (4, 0.6) 2 (3, 7.1) 3 (1, 6.3) (4, 10) (5, 12) 4 5 G[0] G[3] G[1] G[2] 4, 2.3 4, 10 2, 4.5 3, 7.1 1, 3.0 1, 6.3 5, 12 4, 0.6 (G[3])[5] = 12 (G[1])[4] = 0.6 11/10/2018 ACM-ICPC
20
Some basic graph operations
Remove a vertex 2 4 7 1 2 3 4 5 6 7 8 9 1 3 4 5 remove 7 1 7 3 4 6 1 6 9 5 9 11/10/2018 ACM-ICPC
21
Given a graph, determine is the graph is acyclic
2 4 7 9 1 3 5 8 Not An acyclic Graph 6 No vertex in this cycle has in-degree 0 11/10/2018 ACM-ICPC
22
Procedure to determine if a given graph is acyclic
Repeatedly remove vertices with in-degree 0; 2 4 7 9 1 3 5 8 If we can’t remove all vertices, then the graph is not acyclic 6 11/10/2018 ACM-ICPC
23
Procedure to determine if a given graph is acyclic
Repeatedly remove vertices with in-degree 0; 2 4 7 9 1 3 5 8 If all vertices can be removed, then the given graph is acyclic 6 11/10/2018 ACM-ICPC
24
Topological Sort (topological order)
Let G = (V, E) be a directed graph with |V| = n. A Topological Sort is a sequence v1, v2,.... vn, such that { v1, v2,.... vn } = V, and for every i and j with 1 i < j n, (vj , vi) E. 7 1 6 5 3 2 4 7 5 There are more than one such order in this graph!! 2 4 3 1 6 11/10/2018 ACM-ICPC
25
If the graph is not acyclic, then there is no topological sort for the graph.
Not an acyclic Graph 2 4 7 9 1 3 5 8 No way to arrange vertices in this cycle without pointing backwards 6 11/10/2018 ACM-ICPC
26
Algorithm for finding Topological Sort
Principle: vertex with in-degree 0 should be listed first. 2 1 7 2 7 Algorithm: 1 Input G=(V,E) Repeat the following steps until no more vertex left: 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. Process v 11/10/2018 ACM-ICPC
27
Topological Sort (topological order) of an acyclic graph
7 1 6 5 3 2 4 7 5 2 4 3 1 6 11/10/2018 ACM-ICPC
28
Fail to find a topological sort (topological order)
1 2 8 2 4 7 9 1 3 5 8 the graph is not acyclic 6 11/10/2018 ACM-ICPC
29
Topological Sort (topological order) of an acyclic graph
2 8 5 4 3 7 9 6 1 2 4 7 9 1 3 5 8 6 This graph is acyclic 11/10/2018 ACM-ICPC
30
Common Algorithm Techniques
Back Tracking.... Greedy Algorithms: Algorithms that make decisions based on the current information; and once a decision is made, the decision will not be revised Divide and Conquer.... Dynamic Programming..... Problem: Minimized the number of coins for change. In real life, we can use a greedy algorithm to obtain the minimum number of coins. But in general, we need dynamic programming to do the job 11/10/2018 ACM-ICPC
31
Greedy Algorithms Dynamic Programming
Problem: Minimized the number of coins for 66¢ { 25¢, 12¢, 5¢, 1¢ } { 25¢, 10¢, 5¢, 1¢ } 25¢ 2 50¢ 10¢ 1 5¢ 1¢ 5 66¢ 25¢ 2 50¢ 12¢ 1 5¢ 0¢ 1¢ 4 4¢ 7 66¢ 25¢ 2 50¢ 12¢ 0¢ 5¢ 3 15¢ 1¢ 1 6 66¢ 16¢ 16¢ 6¢ 4¢ 1¢ 4¢ 0¢ 0¢ Greedy method Greedy method Dynamic method 11/10/2018 ACM-ICPC
32
Finding the shortest path between two vertices
Starting Vertex Starting Vertex Unweighted Graphs Weighted Graphs 2 5 1 2 2 3 3 5 1 1 1 5 3 3 7 7 7 2 2 1 9 10 12 8 8 2 Both are using the greedy algorithm. w w Final Vertex Final Vertex 11/10/2018 ACM-ICPC
33
Construct a shortest path Map
Starting Vertex Starting Vertex a 0,-1 b c 1,a 1,a d e 2,a 2,a f 2,a x w-1,y n w,x Final Vertex Final Vertex 11/10/2018 ACM-ICPC
34
Construct a shortest path Map for weighted graph
Starting Vertex Starting Vertex 0,-1 2 5 1 1 2 2,0 3,1 5,0 1 5 3 4 7 3,1 7,1 1 9 5 10,2 8,4 w-1,y : decided n w,x Final Vertex Final Vertex 11/10/2018 ACM-ICPC
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.