Graph Algorithms - UBC Department of CPSC Undergraduates
Transcript of Graph Algorithms - UBC Department of CPSC Undergraduates
CPSC 490 – 2011W T2
CPSC 490: Student Directed Seminar
Graph Algorithms
CPSC 490 – 2011W T2
Graph Algorithms
My assumptions You know what undirected and directed graphs are. You are familiar with both the adjacency list and
adjacency matrix representations for graphs. You can write breadth-first and depth-first search
and use them to solve simple problems. Most of you are familiar with greedy algorithms.
CPSC 490 – 2011W T2
Graph Algorithms
My goals: Review very quickly some of the graph algorithms
you may be familiar with. Discuss classes of graph problems that
are useful in practice, and have an efficient solution that can be implemented
reasonably simply.
You will hopefully look at some of these in more details in the next couple of weeks.
CPSC 490 – 2011W T2
Graph Algorithms
Outline Common problems on graphs Perfect graphs Graph drawing
CPSC 490 – 2011W T2
Graph Algorithms
Graph Problems you are most likely already familiar with:
Shortest paths: given a graph G where each edge e has a weight w(e), and a vertex s, find the path with smallest cost from s to every other vertex of G.
Can be solved using Dijkstra's (greedy) algorithm. The Bellman-Ford dynamic programming algorithm works
even when w(e) may be negative. Either it produces the paths, or It determines that the graph has a cycle with negative weight.
CPSC 490 – 2011W T2
Graph Algorithms
Graph Problems (continued) Minimum spanning tree: find a subgraph T of G
that contains every vertex of G, is a tree, and with the minimum sum of edge weights.
Can be solved using either Prim-Jarník or Kruskal's algorithm.
A very complicated, almost linear-time algorithm due to Chazelle also exists.
CPSC 490 – 2011W T2
Graph Algorithms
Definitions: An induced subgraph of a graph G = (V, E) is a
subset V' of V, along with every edge of G whose endpoints are both in V'.
Kn is the graph with n vertices where every two
vertices are connected by an edge.
K3
K4
K5
CPSC 490 – 2011W T2
Graph Algorithms
Other easily-defined, but not as easily solved graph problems:
Clique: find the largest subgraph of G whose vertices are all adjacent.
That is, we want to find the largest induced Kn subgraph.
Independent Set: find the largest induced subgraph of G where no two vertices are adjacent.
That is, we want to find the largest induced subgraph whose complement is K
n.
CPSC 490 – 2011W T2
Graph Algorithms
Other easily-defined, but not as easily solved graph problems (continued):
Partition into cliques: partition the vertices of G into as few subsets V
1, ..., V
k as possible, where for
each i the subgraph induced by Vi is a clique.
Partition into independent sets: same but replace “clique” by “independent set”.
This is graph coloring! Each independent set corresponds to a color. The minimum number of colors is denoted by χ(G).
CPSC 490 – 2011W T2
Graph Algorithms
These problems have many applications. For instance, graph coloring:
Sudoku Scheduling Mobile radio frequency assignment Pattern matching Register allocation in optimizing compilers
CPSC 490 – 2011W T2
Graph Algorithms
Unfortunately, all 4 problems are NP-Complete in general.
There are however classes of graphs for which the first four can be solved in polynomial time.
The biggest one of them is that of perfect graphs. The algorithms that can handle all perfect graphs
are based on linear programming. Subclasses of these graphs admit simpler
algorithms.
CPSC 490 – 2011W T2
Graph Algorithms
Outline Common problems on graphs Perfect graphs Graph drawing
CPSC 490 – 2011W T2
Graph Algorithms
Perfect graph: a graph G is perfect if, for every induced subgraph H of G, χ(H) is equal to the size of H's largest clique.
Strong perfect graph theorem (2002): a graph is perfect if and only if it does not contain an odd hole or an odd antihole with at least 5 vertices.
An odd hole is an induced subgraph with an odd number of vertices that's a cycle.
An odd antihole is the complement of an odd hole.
CPSC 490 – 2011W T2
Graph Algorithms
Definitions: An intersection graph is one whose vertices are
objects in the plane (or d-dimensional space), and where an edge joins two objects if they intersect.
An interval graph is the intersection graph for a group of intervals along the x-axis.
A chordal graph is a graph that does not contain any hole with more than 3 vertices.
It's also an intersection graph of connected subgraphs of a tree.
CPSC 490 – 2011W T2
Graph Algorithms
Interval graphs and chordal graphs are perfect.
There are simple greedy algorithms for our four problems for both types of graphs.
Assume you are given the intervals There is an algorithm to find a collection of intervals (open
or closed) given the graph.
Let's look at interval graphs. Maximum independent set (assume open intervals):
You've seen this problem before. It's the interval scheduling problem!!!
CPSC 490 – 2011W T2
Graph Algorithms
Coloring interval graphs: the algorithm Assume the intervals are open L ← list of colors currently available. For each interval endpoint x, from left to right:
Assume right endpoints at x occur before left endpoints. If x is a right endpoint, add the color of x's interval to L. If x is a left endpoint
choose a color c in L, or a new color c if L is empty. color x's interval using color c
CPSC 490 – 2011W T2
Graph Algorithms
Coloring interval graphs: correctness Suppose we used t colors in total. When we introduced the last color at a point x,
there were already t – 1 intervals that contain x. Hence the graph has a clique with t vertices. Thus t was the smallest number of colors we could
use.
CPSC 490 – 2011W T2
Graph Algorithms
Outline Common problems on graphs Perfect graphs Graph drawing
Reference: Graph Drawing: Algorithms for the visualization of graphs, by
Giuseppe Di Battista, Peter Eades, Roberto Tamassia and Ioannis G. Tollis, Prentice Hall, ISBN 0-13-301615-3, 1999.
CPSC 490 – 2011W T2
Graph Algorithms
Why is graph drawing important? Graphs express relationships between entities. Humans are better at understanding information
when it is presented visually. UML diagrams Links between web pages Classes of graphs Etc.
So we want to determine the “best” way to place vertices and edges on the screen.
CPSC 490 – 2011W T2
Graph Algorithms
Types of drawings: Edges can be
Polygonal chains Straight lines Horizontal or vertical lines Oriented in a fixed direction (for directed graph), for
instance downward or upward. Vertices can be placed
At arbitrary points At integer coordinates
CPSC 490 – 2011W T2
Graph Algorithms
What criteria should we use to determine if a layout is good?
Small number of edge crossings Minimizing the # of edges crossings is NP-Complete.
Minimum area More compact layouts fit better on the screen/on paper.
Minimum total or maximal edge length Very long edges tend not to look good.
Angular resolution Really small angles are hard to view.
CPSC 490 – 2011W T2
Graph Algorithms
Criteria (continued) Aspect ratio
Very skinny drawings are harder to fit on screen/paper. Symmetry
If the graph has symmetries (or some other structure) then they should be easily observed.
It's not possible to meet all criteria at the same time, so we need to prioritize them.
CPSC 490 – 2011W T2
Graph Algorithms
Algorithmic approaches Every algorithm is divided into steps, each of which
aims at satisfying some of the esthetic criteria. There are several different ways to approach the
problem: The hierarchical approach (for directed acyclic graphs).
More details to come. The augmentation approach
Add edges and/or vertices to get a graph with a stronger structure.
This makes it easier to draw.
CPSC 490 – 2011W T2
Graph Algorithms
Approaches (continued) Force-directed approach
Simulate a system of springs on the input graph. Generally easy to understand and code. Tends to give highly symmetric drawings.
Divide and Conquer approach Divide the graph into smaller subgraph. Find a layout for each subgraph recursively. Then connect the subgraphs together.
CPSC 490 – 2011W T2
Graph Algorithms
Example: drawing rooted trees Layering step: assign a layer to each vertex, based
on its distance from the root. Divide and conquer step: for a tree T
If T is a single vertex, then its drawing is trivial. Otherwise
First draw all of the children of T. Then move the drawing of the subtrees towards each other until
their horizontal distance becomes 2. Finally place the root of T one unit above the roots of the
subtrees, somewhere between the leftmost and rightmost children.
CPSC 490 – 2011W T2
Graph Algorithms
The output:
CPSC 490 – 2011W T2
Graph Algorithms
The same tree, with a radial layout: layers are concentric circles around the root.
CPSC 490 – 2011W T2
Graph Algorithms
The same tree, using a spring model.
CPSC 490 – 2011W T2
Graph Algorithms
The hierarchical approach in details: Commonly used for upward or downward drawings
of directed acyclic graphs. Steps:
Layer assignment: assign a layer Li to each vertex v
i. If
there is an edge (vi,v
j) then L
j < L
i. The layers will become
the vertices' y-coordinates. Adding dummy vertices: if an edge goes from one layer
to another layer that is not adjacent, we add dummy vertices so every edge connects vertices in adjacent layers.
CPSC 490 – 2011W T2
Graph Algorithms
Steps (continued): Cross reduction: order the vertices on each layer to
minimize the number of crossings. Assign x-coordinates: we decide where each vertex
goes on its row, and then remove the dummy vertices. We can choose x-coordinates to reduce the drawing's area. When we remove dummy vertices, some edges will be polygonal
lines; we place the vertices to reduce the number of bends in these edges.
CPSC 490 – 2011W T2
Graph Algorithms
Note: in many cases, the problem of getting the “best” result is NP-Complete.
Interesting example: There is an O(n) time algorithm to find a planar
drawing (one with no edge crossing) if one exists. We can also determine if it's possible to draw a
directed graph with all edges going “up” (or “down”) in polynomial time.
Determining if both can be done at the same time is NP-Complete.