1 / 34

Graphs: As a mathematics branch

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

Download Presentation

Graphs: As a mathematics branch

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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 1707-1783 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. 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 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 ACM-ICPC

  5. Weighted Graphs Every edge in E has a value associated to it. 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 ACM-ICPC

  6. A walk on a graph (v1, v2,....vn) Vn , such that (vi , vi+1)  E, for 1 i < n, is calledawalk of G= (V,E) Example: 0 (1,2,4,7,9,8,5,4,7,6,5,2,3) 2 4 awalk 7 9 1 3 5 8 6 ACM-ICPC

  7. More Terminologies (v1, v2,....vn) Vn , such that (vi , vi+1)  E, for 1 i < n, is called awalk of G= (V,E) • Apathis 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 =vnis 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. ACM-ICPC

  8. A Walk: 1,2,4,7,9,8,5,4,7,6,5,2,3 (not a trial) 0 2 4 7 9 1 3 5 8 6 ACM-ICPC

  9. 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) 0 2 4 7 9 1 3 5 8 6 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 0 2 4 7 9 1 3 5 8 6 ACM-ICPC

  11. A graph without any cycle in it is called acyclic 0 2 4 7 9 1 3 5 8 6 An acyclic Graph ACM-ICPC

  12. Representation of Graphs. (Data Structures) Adjacency Matrix Good for dense graph. 0 2 4 7 9 1 3 5 8 6 ACM-ICPC

  13. Representation of Graphs. Adjacency Matrix for a Weighted Graph 0 2.3 3 1 2/3 4 5 4.5 10 12 2 7.1 3 ACM-ICPC

  14. Representation of Graphs. (Data Structures) Adjacency List A commonly used representation 0 2 4 7 9 1 3 5 8 6 ACM-ICPC

  15. Representation of Graphs. Adjacency List for a Weighted Graph 0 2.3 3 1 2/3 4 5 4.5 10 12 2 7.1 3 How to implement? 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. vector deque stack set map array (dynamic) linked lists ACM-ICPC

  17. <set> 10 Red-Black Tree #include <iostream> #include <set> .............. set<int> S; .............. while (....) { int e; // an element of S S.insert(e); 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 ACM-ICPC

  18. <map> 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); 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 ACM-ICPC

  19. Using STL to implement Graphs typedef map<int, double> AdjacencyList; typedef map<int, AdjacencyList> Graph; 0 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, 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 ACM-ICPC

  20. Remove a vertex Some basic graph operations remove 7 ACM-ICPC

  21. Given a graph, determine is the graph is acyclic 0 2 4 7 9 1 3 5 8 Not An acyclic Graph 6 No vertex in this cycle has in-degree 0 ACM-ICPC

  22. Procedure to determine if a given graph is acyclic Repeatedly remove vertices with in-degree 0; 0 2 4 7 9 1 3 5 8 If we can’t remove all vertices, then the graph is not acyclic 6 ACM-ICPC

  23. Procedure to determine if a given graph is acyclic Repeatedly remove vertices with in-degree 0; 0 2 4 7 9 1 3 5 8 If all vertices can be removed, then the given graph is acyclic 6 ACM-ICPC

  24. Let G = (V, E) be a directed graph with |V| = n. Topological Sort (topological order) 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 0 5 3 2 4 0 7 5 There are more than one such order in this graph!! 2 4 3 1 6 ACM-ICPC

  25. If the graph is not acyclic, then there is no topological sort for the graph. 0 Not an acyclic Graph 2 4 7 9 1 3 5 8 No way to arrange vertices in this cycle without pointing backwards 6 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 vwith in-degree 0; If can’t find such vand V is not empty then stop the algorithm; this graph is not acyclic 2. Remove vfrom V and update E 3. Process v ACM-ICPC

  27. Topological Sort (topological order) of an acyclic graph 7 1 6 0 5 3 2 4 0 7 5 2 4 3 1 6 ACM-ICPC

  28. Fail to find a topological sort (topological order) 1 2 8 0 2 4 7 9 1 3 5 8 the graph is not acyclic 6 ACM-ICPC

  29. Topological Sort (topological order) of an acyclic graph 0 2 8 5 4 3 7 9 6 1 0 2 4 7 9 1 3 5 8 6 This graph is acyclic 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 ACM-ICPC

  31. Greedy Algorithms  Dynamic Programming Problem: Minimized the number of coins for 66¢ { 25¢, 12¢, 5¢, 1¢ } { 25¢, 10¢, 5¢, 1¢ } 16¢ 16¢ 6¢ 4¢ 1¢ 4¢ 0¢ 0¢ Greedy method Greedy method Dynamic method ACM-ICPC

  32. Finding the shortest path between two vertices Starting Vertex Starting Vertex Unweighted Graphs  Weighted Graphs 0 0 2 5 1 2 2 3 3 5 1 1 1 5 3 3 7 7 7 2 2 1 9 12 8 8 10 2 Both are using the greedy algorithm. w w Final Vertex Final Vertex 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 ACM-ICPC

  34. Construct a shortest path Map for weighted graph  Starting Vertex Starting Vertex  0 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 ACM-ICPC

More Related