1 / 38

Graph Algorithms Exploration - Computer Science Lecture

Dive into graph algorithm basics and techniques including BFS, DFS, spanning trees, shortest paths, and flow problems. Learn important concepts and their applications.

ecourtney
Télécharger la présentation

Graph Algorithms Exploration - Computer Science Lecture

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. Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn Office: Rm430, Computing Center

  2. Text Book & Reference Books • Text Book: • Introduction to Algorithms (Second Edition) • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein • Higher Education Press, The MIT Press, 68RMB • Reference Books • Algorithm Design Techniques and Analysis • M.H. Alsuwaiyel (Saudi Arabia), 电子工业出版社,40元。 • 《图算法》:马绍汉编著,山东大学出版社。 • 《算法设计与分析》:王晓东编著,清华大学出版社,30元。 • 《计算机算法导引—设计与分析》:卢开澄编著,清华大学出版社,21元。

  3. Grading Scheme • Total 100 • Assignment 10% • Final Examination: 90%

  4. Contents • Part I • Explore techniques on a graph • Breadth-first search algorithm (BFS) • Depth-first search algorithm (DFS) • Applications of DFS • Classifying edges (for directed or undirected) • Topological sort (for directed acyclic) • Strongly connected components decomposing (for directed) • Bi-connected components decomposing (for undirected)

  5. Contents (continued) • Part II • Minimum spanning tree problem (for undirected) • Kruskal’s algorithm • Prim’s algorithm • Bottleneck spanning tree problem • Part III • Single-source shortest paths problem (for directed) • Bellman-Ford algorithm • Dijkstra’s algorithm • All-pairs shortest paths problem • Matrix-multiplication based algorithm • Floyd’s algorithm • Johnson’s algorithm

  6. Contents (continued) • Part IV • Maximum flow problem (for network) • Ford-Fulkerson algorithm • Dinic’s algorithm • Applications of Max-flow problem • Maximum matching algorithm for Bipartite graphs • Part V • MaximumMatching

  7. Prerequisite • Data Structure • C, Java or other programming languages • A little mathematics

  8. Graph Preliminary Knowledge • In this class, try to answer three questions: • What is a graph? • How to represent a graph? • Basic properties of a graph

  9. Graph Basics (Graph) • A graphG is a pair (V, E), where V is a finite set and E is a set of pairs on V • Denote as G = (V, E) • Element of V: Vertex; • Element of E: Edge; • Undirected graph (u. w. l. g., graph): Each pair in E is unordered, i.e., for each pair (u, v) E, (u, v) = (v, u). • Directed graph: Each pair in E is ordered, i.e., for each pair (u, v)  E, (u, v)  (v, u), and either (v, u)  E or not. • G1 = ((1, 2, 3), ((1, 2), (2, 3), (3,1))) • G2 = ((1, 2, 3), ((1, 2), (2, 3), (1,4)))?

  10. e6 e1 v1 v2 v5 e2 e3 e7 e4 v4 v3 v6 e5 Schematic Representation e1 v1 v2 v5 e2 e7 e3 e4 v4 v3 v6 Can you give the representation in format G = (V, E) for above?

  11. Graph Basics (Adjacency) • Adjacency: In a graph G = (V, E), if (u, v)  E, say that vertex v is adjacent to vertex u. • If G is an undirected graph, vertex u is adjacent to vertex v, too. The adjacency relation is symmetric. We also say (u, v) is incident withu and v, vice versa. • If G is a directed graph, if (u, v)  E, vertex v is adjacent to vertex u, vertex u is not necessarily adjacent to vertex v. (u, v) is incident from or leaves vertex u and is incident to or enters vertex v. • Two edges are called to be adjacent if they share a common vertex.

  12. Graph Basics (Degree) • Degreeof a vertex v: • For undirected graph: • Number of edges incident with v. • Denote as dG(v) or simply, d(v). • For directed graph: • In-degree: Number of edges entering (or incident to) v. • Out-degree: Number of edges leaving (or incident from) v. • Denote as idG(v), odG(v) or simply, id(v), od(v) respectively.

  13. Graph Properties • Theorem 1.1 (Handshaking Lemma) • Given an undirected graph G = (V, E), the sum of degrees of all vertices is that , where | E | is the number of edges in G. • Proof: Can you give? • How about a directed graph? (sum of in-degrees equals to sum of out-degrees, i.e., the number of edges) • Corollary 1.2 • Given a graph G = (V, E), the number of vertices with odd degrees is even. • Proof: Can you give?

  14. Graph Basics (Path) • A path of length k from a vertex u to a vertex u’ in a graph G = (V, E) is a sequence <v0, v1, v2, …, vk> of vertices such that u = v0, u’ = vk, and (vi-1, vi)  E for i = 1, 2, …, k. • Length of a path: Number of edges in the path • u’ is reachable from u via p if there is a path p from u to u’, write as u u’, sometimes. For undirected graph, we also call u and u’ are connected. • Simple path: All vertices in the path are distinct. • A sub-path of path p = <v0, v1, v2, …, vk> is a contiguous subsequence of its vertices. • For any 0  i  j  k, the subsequence of vertices < vi, vi+1, …, vj > p

  15. Graph Basics (Cycle) • A cycle is a path p = <v0, v1, v2, …, vk> with v0 = vk. • For undirected graph, a simple cycle contains at least 3 edges, that is, k 3 and v1, v2, …, vk are distinct. • For directed graph, a simple cycle contains at least 1 edge, and v1, v2, …, vkare distinct. • Acyclic graph: A graph with no cycles. • DAG: Directed acyclic graph.

  16. Graph Basics (Sub-graph) • A graph G’ = (V’, E’) is a sub-graph of G = (V, E) if V’V and E’ E. • A graph G’’ = (V’’, E’’) is a vertex induced sub-graph of G = (V, E) if V’’V and E’’ = {(u, v)  E: u, v V’’}. G G’ G’’

  17. Graph Basics (Connected) • An undirected graph is connected if every pair of vertices is connected by a path (reachable from each other). • Connected component of a graph is a sub-graph such that: 1) It is connected; 2) Any other sub-graph that contains it as a true sub-graph is not connected. • Any connected, undirected graph G = (V, E) satisfies that | E |  | V | -1. (Hints: Adding each edge decreases # of connected components by at most one) • A directed graph is strongly connected if every two vertices are reachable from each other. • Strongly connected component of a directed graph is a sub-graph such that: 1) It is strongly connected; 2) Any other sub-graph that contains it as a true sub-graph is not strongly connected.

  18. Connectedness (exa.) G F Other SCCs?

  19. Special graphs • A complete graph is an undirected graph in which every pair of vertices are adjacent. • A bipartite graph is a graph G = (V, E) in which V can be partitioned into two disjoint sets V1 and V2 such that (u, v)  E implies either uV1 and vV1 or uV2 and vV2. • A forest is an acyclic, undirected graph. • A tree is a connected, acyclic, undirected graph. (|E|=|V|-1) (proof? Induction on # of vertices)

  20. Sparse vs. Dense • Sparse graph: | E | is much less than | V |2 • Dense graph: | E | is close to | V |2 • “Spare” and “dense” are relative concepts.

  21. Asymptotic Notation (O) • O-notation: Asymptotic upper bound • O(g(n)) = {f(n): there exist positive constants c and n0 such that 0  f(n)  cg(n) for all n  n0}. • f(n) = 2n3+3n-5(or =) O(n3) • f(n) = 2n3+3n-5(or =)O(n4)

  22. cg(n) f(n) n n0 Asymptotic Notation (O) f(n) = O(g(n))

  23. Asymptotic Notation () • -notation: Asymptotic lower bound • (g(n)) = {f(n): there exist positive constants c and n0 such that 0  cg(n)  f(n)for all n  n0}. • f(n) = 2n3+3n-5 (or =)(n3) • f(n) = 2n3+3n-5 (or =)(n2)

  24. Asymptotic Notation () f(n) cg(n) n n0 f(n) = (g(n))

  25. Asymptotic Notation () • -notation: Exact order • (g(n)) = {f(n): there exist positive constants c1, c2, and n0 such that 0  c1g(n)  f(n)  c2g(n) for all n  n0}. • f(n) = 2n3+3n-5 (or =) (n3)

  26. Asymptotic Notation () c2g(n) f(n) c1g(n) n n0 f(n) = (g(n))

  27. Representations of graphs • Two standard ways: • A collection of adjacency lists • An adjacency matrix

  28. Adjacency-list representation • Consist of an array Adj of | V | lists, one for each vertex in V. • Adjacency list Adj[u] contains all the vertices v such that there is an edge (u, v)  E, i.e., all vertices that are adjacent to u. • Vertices in Adj[u] can be stored in arbitrary order. • Sum of the lengths of all the adjacency lists is • | E | if G is a directed graph • 2| E | if G is an undirected graph • Memory needed to store an adjacency-list representation of a graph is (| V | + | E |) • Weighted graph can be represented by adjacency lists.

  29. Representations of graphs (Examples)

  30. Representations of graphs (Examples)

  31. Adjacency-list representation (advantage vs. disadvantage) • Advantage: • When the graph is sparse, uses only O(| V | + | E |) memory. • Disadvantage: • No quicker way to determine if a given edge (u, v) is present in the graph than to search for v in the adjacency list Adj[u]. (How much time needed?)

  32. Adjacency-matrix representation • Consists of a | V |  | V | matrix A = (aij) such that or A is symmetric along the main diagonal for undirected graphs, that is, AT = A. For directed graph, generally, AT A. Can you imagine what directed graph satisfies AT = A? Adjacency-matrix can be used to represent weighted graphs.

  33. Adjacency-matrix representation (Advantage vs. disadvantage) • Advantage: • Easily or quickly to determine if an edge is in the graph or not by just looking at the proper position of the matrix. • Disadvantage: • Uses more memory to store a graph. O(| V |2)

  34. Properties of adjacency-matrix representation • Lemma 1.3 • Suppose that A is the adjacency-matrix representation of an undirected graph G, then the element Ak[i, j] of Ak = AA… A is the number of paths of length k that connect vertices i and j. • Proof by induction on k. • Basic step: k = 1, A1 = A, A[i, j] is the number of paths of length 1 that connect vertices i and j. • Inductive step: Suppose it is true for k >1 and the numbers smaller than k, that is, Ak[i, j] is the number of paths of length k that connect vertices i and j. From Ak+1=A  Ak, we have Ak+1[i, j] = lV A[i, l]  Ak[l, j]. For A[i, l] is the number of paths of length 1 that connect vertices i and l, Ak[l, k] is the number of paths of length k that connect vertices l and j. So, A[i, l]  Ak[l, j] is the number of paths of length k+1 that connect vertices i and j via l. Consider all vertices l, 1 l  n, we get our conclusion.

  35. Properties of adjacency-matrix representation Corollary 1.4 Proof: For symmetry of adjacency-matrix, A[i, l]=A[l, i] = 0 or 1, so A2[1, 1]: 2: (1, 2, 1), (1, 3, 1) A2[2, 2]: 3: (2, 1, 2), (2, 3, 2), (2, 4, 2) 1 2 3 4

  36. Euler Graph In Konigsberg, German, a river ran through the city such that in its center was an island, and after passing the island, the river broke into two parts. Seven bridges were built so that the people of the city could get from one part to another. • History: • Leonhard Euler (1707 -1783)

  37. Knigsberg Bridges Problem People wondered whether or not one could walk around the city in a way that would involve crossing each bridge exactly once. Now, have a try! How about if a bridge is removed?

  38. Why?

More Related