06 Basic Graph Algorithms

download 06 Basic Graph Algorithms

of 31

Transcript of 06 Basic Graph Algorithms

  • 8/12/2019 06 Basic Graph Algorithms

    1/31

    CS 97SI: INTRODUCTION TOPROGRAMMING CONTESTS

    Jaehyun Park

  • 8/12/2019 06 Basic Graph Algorithms

    2/31

    Todays Lecture: Graph Algorithms

    What are graphs?

    Adjacency Matrix and Adjacency List

    Special Graphs

    Depth-First Search and Breadth-First Search

    Topological Sort

    Eulerian Circuit

    Minimum Spanning Tree (MST)

    Strongly Connected Components (SCC)

  • 8/12/2019 06 Basic Graph Algorithms

    3/31

    What are graphs?

    An abstract way of representing connectivity usingnodes (or vertices) and edges

    We will label the nodes from 1 to

    edges connect some pairs of nodes

    Edges can be eitherone-directional (directed)

    or bidirectional Nodes and edges can have

    some auxiliary information

    Figure from Wikipedia

  • 8/12/2019 06 Basic Graph Algorithms

    4/31

    Why study graphs?

    Lots of problems formulated and solved in terms ofgraphs

    Shortest path problems

    Network flow problems

    Matching problems

    2-SAT problem

    Graph coloring problem Traveling Salesman Problem (TSP): still unsolved!

    and many more

  • 8/12/2019 06 Basic Graph Algorithms

    5/31

    Storing Graphs

    We need to store both the set of nodes and theset of edges

    Nodes can be stored in an array

    Edges must be stored in some other way

    We want to support the following operations

    Retrieving all edges incident to a particular node

    Testing if given two nodes are directly connected Use either adjacency matrix or adjacency list to

    store the edges

  • 8/12/2019 06 Basic Graph Algorithms

    6/31

    Adjacency Matrix

    An easy way to store connectivity information

    Checking if two nodes are directly connected: 1 time

    Make an matrix = 1if there is an edge from to

    = 0otherwise

    Uses 2 memoryOnly use when is less than a few thousands,

    AND when the graph is dense

  • 8/12/2019 06 Basic Graph Algorithms

    7/31

    Adjacency List

    Each node has its own list of edges

    The lists have variable lengths

    Space usage: ( + )

    1

    2

    3

    4

    5

    From To

    1 2 3 5

    2 3 5

    3 2

    4 2 5

    5

  • 8/12/2019 06 Basic Graph Algorithms

    8/31

  • 8/12/2019 06 Basic Graph Algorithms

    9/31

    Implementation Using Arrays

    ID To Next Edge ID

    1 2 -

    2 3 -

    3 3 14 5 3

    5 3 -

    6 2 -

    7 2 5

    8 5 2

    1

    2

    3

    4

    5

    From 1 2 3 4 5

    Last Edge ID 4 8 6 7 -

    1 2

    3

    4

    56

    78

  • 8/12/2019 06 Basic Graph Algorithms

    10/31

    Implementation Using Arrays

    Have two arrays Eof size and LEof size

    Econtains the edges

    LEcontains the starting pointers of the edge lists

    Initialize LE[i] = -1for all i

    LE[i] = 0is also fine if the arrays are 1-indexed

    Inserting a new edge from uto vwith ID k

    E[k].to = v E[k].nextID = LE[u]

    LE[u] = k

  • 8/12/2019 06 Basic Graph Algorithms

    11/31

    Implementation Using Arrays

    Iterating over all edges starting at u: for(ID = LE[u]; ID != -1; ID = E[ID].nextID)

    // E[ID] is an edge starting from u

    Its pretty hard to modify the edge lists

    The graph better be static!

  • 8/12/2019 06 Basic Graph Algorithms

    12/31

    Special Graphs

    Tree: a connected acyclic graph

    The most important type of graph in CS

    Alternate definitions (all are equivalent!)

    A connected graph with 1edges An acyclic graph with 1edges

    There is exactly one path between every pair of nodes

    An acyclic graph but adding any edge results in a cycle

    A connected graph but removing any edge disconnects it

  • 8/12/2019 06 Basic Graph Algorithms

    13/31

    Special Graphs

    Directed Acyclic Graph (DAG): the name says whatit is

    Equivalent to a partial ordering of nodes

    Bipartite Graph

    Nodes can be separated into two groups and such

    that edges exist between and only (no edges withinor within )

  • 8/12/2019 06 Basic Graph Algorithms

    14/31

    Graph Traversal

    The most basic graph algorithm that visits nodes ofa graph in certain order

    Used as a subroutine in many other algorithms

    We will cover two algorithms

    Depth-First Search (DFS): uses recursion (stack)

    Breadth-First Search (BFS): uses queue

  • 8/12/2019 06 Basic Graph Algorithms

    15/31

    Depth-First Search Pseudocode

    DFS(): visits all the nodes reachable from indepth-first order

    Mark as visited

    For each edge : If is not visited, call DFS()

    Use non-recursive version if recursion depth is toobig (over a few thousands)

    Replace recursive calls with a stack

  • 8/12/2019 06 Basic Graph Algorithms

    16/31

    Breadth-First Search Pseudocode

    BFS(): visits all the nodes reachable from inbreadth-first order

    Initialize a queue

    Mark as visited and push it to While is not empty:

    Take the front element of and call it

    For each edge :

    If is not visited, mark it as visited and push it to

  • 8/12/2019 06 Basic Graph Algorithms

    17/31

    Topological Sort

    Input: a DAG = ,

    Output: an ordering of nodes such that for eachedge , comes before

    There can be many answers

    e.g. {6, 1, 3, 2, 7, 4, 5, 8}and {1, 6, 2, 3, 4, 5, 7, 8}

    are valid orderings forthe graph on the right 1

    2

    4

    8

    6

    73

    5

  • 8/12/2019 06 Basic Graph Algorithms

    18/31

    Topological Sort

    Any node without an incoming edge can be the firstelement

    After deciding the first node, remove outgoing

    edges from it Repeat!

    Time complexity: 2 + Ugh, too slow

  • 8/12/2019 06 Basic Graph Algorithms

    19/31

    Topological Sort (faster version)

    Precompute the number of incoming edges deg for each node

    Put all nodes with zero deg into a queue

    Repeat until becomes empty:

    Take from

    For each edge

    Decrement deg (essentially removing the edge ) If deg becomes zero, push to

    Time complexity: +

  • 8/12/2019 06 Basic Graph Algorithms

    20/31

    Eulerian Circuit

    Given an undirected graph

    We want to find a sequence of nodes that visitsevery edge exactly once and comes back to the

    starting point Eulerian circuits exist if and only if

    is connected

    and each node has an even degree

  • 8/12/2019 06 Basic Graph Algorithms

    21/31

    Constructive Proof of Existence

    Pick any node in and walk randomly(!) withoutusing the same edge more than once

    Each node is of even degree, so when you enter a

    node, there will be an unused edge you exit through Except at the starting point, at which you can get stuck

    When you get stuck, what you have is a cycle

    Remove the cycle and repeat the process in eachconnected component

    Glue the cycles together to finish!

  • 8/12/2019 06 Basic Graph Algorithms

    22/31

    Related Problems

    Eulerian path: exists if and only if the graph isconnected and the number of nodes with odddegree is 0 or 2.

    Hamiltonian path/cycle: a path/cycle that visitsevery nodein the graph exactly once. Looks similar

    but still unsolved!

  • 8/12/2019 06 Basic Graph Algorithms

    23/31

    Minimum Spanning Tree (MST)

    Given an undirected weighted graph = ,

    Want to find a subset of with the minimum totalweight that connects all the nodes into a tree

    We will cover two algorithms:

    Kruskalsalgorithm

    Prims algorithm

  • 8/12/2019 06 Basic Graph Algorithms

    24/31

    KruskalsAlgorithm

    Main idea: the edge with the smallest weight hasto be in the MST

    Simple proof:

    Assume not. Take the MST that doesnt contain . Add to , which results in a cycle.

    Remove the edge with the highest weight from the cycle.

    The removed edge cannot be since it has the smallest weight.

    Now we have a better spanning tree than Contradiction!

  • 8/12/2019 06 Basic Graph Algorithms

    25/31

    KruskalsAlgorithm

    Another main idea: after an edge is chosen, the twonodes at the ends can be merged and consideredas a single node (supernode)

    Pseudocode: Sort the edges in increasing order of weight

    Repeat until there is one supernode left:

    Take the minimum weight edge

    If connects two different supernodes:

    Connect them and merge the supernodes (use union-find)

    Otherwise, ignore and go back

  • 8/12/2019 06 Basic Graph Algorithms

    26/31

    Prims Algorithm

    Main idea:

    Maintain a set that starts out with a single node

    Find the smallest weighted edge = , that

    connects and Add to the MST, add to

    Repeat until =

    Differs from Kruskalsin that we grow a singlesupernode instead of growing multiple ones hereand there

  • 8/12/2019 06 Basic Graph Algorithms

    27/31

    Prims Algorithm Pseudocode

    Initialize to , to cost , for every

    If there is no edge between and , cost , =

    Repeat until = :

    Find with smallest Use a priority queue or a simple linear search

    Add to , add to the total weight of the MST

    For each edge , :

    Update to min ,cost , Can be modified to compute the actual MST along

    with the total weight

  • 8/12/2019 06 Basic Graph Algorithms

    28/31

    Kruskalsvs Prims

    Kruskals Algorithm

    Takes l og time

    Pretty easy to code

    Generally slower than Prims

    Prims Algorithm

    Time complexity depends on the implementation:

    Can be 2

    + , log , log A bit trickier to code

    Generally faster than Kruskals

  • 8/12/2019 06 Basic Graph Algorithms

    29/31

    Strongly Connected Components (SCC)

    Given a directedgraph = ,

    A graph is strongly connectedif all nodes arereachable from every single node in

    Strongly connected components of are maximalstrongly connectedsubgraphs of

    The graph on the righthas 3 SCCs: {a, b, e},{c, d, h}, {f, g}

    Figure from Wikipedia

  • 8/12/2019 06 Basic Graph Algorithms

    30/31

    KosarajusAlgorithm

    Initialize counter = 0

    While not all nodes are labeled:

    Choose an arbitrary unlabeled node

    Start DFS from Check the current node as visited

    Recurse on all unvisited neighbors

    After the DFS calls are finished, increment and set s label to

    Reverse the direction of all the edges For node with label 1

    Find all reachable nodes from and group them as an SCC

  • 8/12/2019 06 Basic Graph Algorithms

    31/31

    KosarajusAlgorithm

    We wont prove why this works

    Two graph traversals are performed

    Running time: +

    Other SCC algorithms exist but this one isparticularly easy to code

    and asymptotically optimal