25
1 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected]) Lecture 10 Graph Algorithms Euiseong Seo ([email protected])

Lecture 10 Graph Algorithmselearning.kocw.net/KOCW/document/2015/sungkyunkwan/... · Testing for articulation vertices or bridges •Brute force •Be sure to add that vertex/edge

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • 1 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Lecture 10 Graph Algorithms

    Euiseong Seo

    ([email protected])

  • 2 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Graph Theory

    Study of the properties of graph structures

    • It provides us with a language with which to talk about graphs

    Keys to solving problems

    • Identifying the fundamental graph theoretic notion underlying the situation

    • Using classical algorithms to solve the resulting problem

  • 3 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Degrees

    Number of edges connected to a vertex

    For undirected graphs

    • Sum of all degrees = 2 * edges

    For directed graph

    • Sum of in-degree = sum of out-degree

  • 4 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Connectivity

    A graph is connected if there is an undirected path

    between every pair of vertices

    Existence of a spanning tree -> connectivity

    • BFS or DFS connected component algorithms to find connected components

    Articulation vertex

    • A single vertex whose deletion disconnects the graph

    Biconnected graphs

    • Any graphs without an articulation vertex

    Bridge

    • A single edge whose deletion disconnects the graph

  • 5 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Connectivity

    Testing for articulation vertices or bridges

    • Brute force

    • Be sure to add that vertex/edge back before doing the next deletion!

    Strongly connected components

    • For directed graphs only

    • Partitions the graph into chunks such that there are directed paths between all pairs of vertices within a given chunk

    • Road networks should be strongly connected

  • 6 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Cycles in Graphs

    All non-tree connected graphs contain cycles

    Eulerian cycle

    • A tour which visits every edge of the graph exactly once

    • Condition for a undirected graph to have an Eulerian cycle?

    • Find an Eulerian cycle by building it once cycle at a time – Finding a back edge using DFS

    – Deleting the edges on this cycle leaves each vertex with even degree

  • 7 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Cycles in Graphs

    Hamiltonian cycles

    • A tour which visits every vertex of the graph exactly once

    • Traveling salesman problem = the shortest Hamiltonian cycle on a weighted graph

    • No efficient algorithm to find one – Backtracking!

  • 8 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Spanning Tree

    Given a graph G = (V, E) and tree T = (V, E) – E E

    – for all (u, v) in E u, v V

    – for all connected graph, there exists a spanning tree

    A spanning tree can be constructed using DFS or BFS

    s

    2

    5

    4

    7

    8

    3 6 9

    s

    2

    5

    4

    7

    8

    3 6 9

    0

    1

    1

    1

    2

    2

    3

    3

    3

  • 9 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Minimum Spanning Tree

    A spanning tree whose sum of edge weights is minimal

    Importance of MSTs

    • When to need connect a set of points by the smallest amount of roadway, wire or pipe

    Prim’s and Kruskal’s algorithms

  • 10 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Example of MST: Prim’s Algorithm

  • 11 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    1. Vertex D has been chosen as a starting point ① Vertices A, B, E, F are connected to D through a single edge. ② A is the nearest to D and thus chosen as the 2nd vertex along

    with the edge AD

  • 12 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    2. The next vertex chosen is the vertex nearest to either D or A. So the vertex F is chosen along with the edge DF

  • 13 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    3. same as 2, Vertex B is chosen.

  • 14 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    4. among C, E, G, E is chosen.

  • 15 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    5. among C, G, C is chosen.

  • 16 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    6. G is the only remaining vertex. E is chosen.

  • 17 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    7. The finally obtained minimum spanning tree

    the total weight is 39

  • 18 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Applications of Prim’s

    Maximum spanning trees

    Minimum product spanning trees

    Minimum bottleneck spanning tree

  • 19 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Shortest Path

    For unweighted graphs

    • BFS suffices

    For weighted graphs

    • Dijkstra’s algorithm

    Dijkstra’s algorithm is very similar to Prim’s

  • 20 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Dijkstra’s Algorithm

    s

    3

    t

    2

    6

    7

    4

    5

    24

    18

    2

    9

    14

    15 5

    30

    20

    44

    16

    11

    6

    19

    6

  • 21 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    All Pairs Shortest Path

    When you want to know the center vertex for a new business

    • Dijksta’s?

    We need another one

    • Floyd’s all pairs shortest path algorithm

  • 22 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    All Pairs Shortest Path

    Floyd’s algorithm

    • W(i,j,k) = min(W(i,j,k-1), W(i,k,k-1)+W(k,j,k-1))

  • 23 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Transitive Closure

    Sometimes we are interested in which vertices are

    reachable from a given node

    The Blackmail problem

  • 24 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Network Flow

    Any edge-weighted graph can be thought of as a network of pipes

    • Weight of edge (i, j) measures the capacity of the pipe

    Network flow problem

    • Given source and sink vertices of a graph

    • The maximum amount of flow which can be sent from source to sink while respecting the maximum capacities of each pipe

  • 25 SWE2004: Principles in Programming | Spring 2014 | Euiseong Seo ([email protected])

    Bipartite Graphs

    A graph G is bipartite or two-colorable if the vertices can be divided into two sets

    A matching in a graph G = (V, E) is a subset of edges E’ ⊂ E such that no two edges in E’ share a vertex

    • Job-worker pairs

    • Marriage graphs

    The largest possible bipartite matching can be found using network flow