The most important algorithms of graph theory are polynomial. It is impossible to survey this topic in this course; graph algorithms can fill several books. We restrict ourselves to a brief discussion of a few examples that provide particularly important insight into some basic techniques. We’ll group our examples around two algorithmic methods: searching a graph and augmenting paths.
4.4.1
How is a graph given?
4.4.2
Searching a graph
a. General search and connectivity testing. Perhaps the most fundamental question to ask about a graph is whether or not it is connected. If not, we often want to find its connected components.
These tasks are trivial to do in polynomial time, in a variety of ways. The reason why we describe a solution is because its elements will be useful later.
LetGbe a graph. Select any noder. Build up a tree T as follows. at the beginning,T
consists of justr. At any stage, we look if there is an edge between the nodes of T and the rest of the node. If there is such an edge, we add it toT, together with its endnode outside
T. If not, then we know that G is disconnected and T is a spanning tree of a connected component. We can delete the nodes ofT grom the graph and proceed recursively.
It is costumary to call the nodes ofT labelled. The usual way to look for an edge between a labelled and an unlabelled node is to look at the labelled nodes and investigate the edges
4.4. POLYNOMIAL TIME II: GRAPH ALGORITHMS 69 going out of them. A simple but important observation is that if at one stage we find that none of the edges of a given labelled node goes to an unlabelled node, then we don’t need to investigate this node at any other later stage (since only an unlabelled node can become labelled, not vice versa). Therefore we can mark this node as taken care of, orscanned. At any time, we only need to look at those nodes that are labelled but not scanned.
This general labelling-scanning procedure is calledsearching the graph.
b. Breadth-First-Search and shortest paths. Specializing the order in which we in- vestigate edges out of the labelled nodes, we get special search algorithms that are good for solving different kinds of graph problems. Perhaps the simplest such specialization isdepth- first-search. The problem is to find a shortest path in a graph from a distinguished node s
(the source) to a distinguished nodet(the sink). The solution of this problem is very simple once we embed the problem in the larger family of problems:we ask for the shortest path from the source s to every other node of the graph. Proceed recursively: label the neighbors of the source swith a 1. Label withkthose unlabelled vertices which are neighbors of vertices of label k−1. Then the label of each node is its distance from s. (This method is perhaps the simplest example of a technique calleddynamic programming.
This idea reappears in the more general problem of finding a shortest path fromstotin a graph with weighted edges. (The weightcij on the edge{i, j} is interpreted as the length
of the edge.) Again we embed this problem in the larger family of problems which asks for a shortest (minimum weight) path fromsto every other node of the graph.
Dijkstra’s algorithm recognizes that a path between two vertices may be of minimum weight even if other paths have fewer edges. So if we begin at the source, among all neighbors
jofs, find one such that the edgesjhas minimum weightcsj. We can confidently assert that
the shortest path in the graph from s to this neighbor has length csj, but we are not sure
about the other neighbors ofs. So label this one neighborcsj. You may think ofsas labelled
with a 0. In the course of the algorithm we maintain a set T of vertices, each of which we have confidently labelled with the minimum weight of a path from s to it. Call this label
d(s, j), the distance fromsto the nodej. These minimum weight paths pass only through vertices already inT. At each step of the algorithm, we consider vertices on the frontier ofT
(vertices not inT which are neighbors of vertices inT). Consider edgesij between verticesi
inT and verticesjin this frontier. An upper bound for the minimum weight of a path from
s to j is the smallest of the numbersd(s, i) +cij where i is in T. Find i∈T andj which
minimized(s, i) +cij. We can confidently label such a nodej withd(s, j) =d(s, i) +cij and
addj toT for the following reason: Any path fromstoj must leave the set T at a node i0 then pass through a nodej0of the frontier. Hence this competing path fromstojhas length at least d(s, i0) +c
T
i
j
s
t
Figure 4.1: Dijkstra’s shortest path algorithm.
toj has length no greaterd(s, i0) +c
i0j0 (figure 4.1).
c. Depth-First-Search, 2-connected components, and 3-connected components. d. Depth-First-Search and planarity.
4.4.3
Maximum bipartite matching and alternating paths
LetG be a graph; we want to find a maximum size matching in G(recall that amatching is subset of the edges having mutually no endpoint in common). A matching is perfect if it mmets every node. We describe a polynomial time algorithm that finds a maximum size matching in a bipartite graph.
Assume that we have a matchingM; how can we see whetherMis maximum? One answer to this question is provided by the following simple criterion due to Berge. An alternating path (with respect to M) is a path in which every other edge belongs toM (the path may start with an edge of M or with an edge not in M). An alterning path is augmentingif it starts and ends with an edge not inM, and moreover, its endpoints are not incindent with any edge ofM. (An augmenting path has necessarily odd length.)
4.4. POLYNOMIAL TIME II: GRAPH ALGORITHMS 71 Lemma 4.4.1 A matching inGis maximum if and only if there is no augmenting path with respect to it.
ProofIt is obvious that if we have an augmenting path P with respect toM, then we can improveM: we can replace inM the edges ofP∩M by the edges ofP\M, to get a larger matching.
Conversely, ifM is not maximum, and there exists a larger matching M0, then consider the connected components of M ∪M00. These are either alternating paths or alternating circuits with respect to M. At least one such component must contain more edges of M0 than ofM: this coponent is an augmenting path with respect toM.
This fact is not very deep since it relates the existence of something (a larger matching) to the existence of something else (an augmenting path). But it does give a motivation for most of the known polynomial time matching algorithms: one can look for an augmenting path by building up a search tree.
Given a bipartite graph G= (A, B), we want to find a maximal matching M. We use the notion of alternating trees. Given a matchingM, we call a vertex of the graph exposed (by M) if it is not covered by M. An alternating tree is a subgraph of G which contains exactly one exposed noder; for which every node at odd distance fromrhas degree 2 in the tree; such that along any path from revery other edge belongs to the matching; and whose endpoints are all at even distance fromr.
The vertices of the tree at even distance fromr(includingritself) are calledouternodes, and the vertices at odd distance fromr are calledinnernodes.
Analternating forest is a forest such that every component is an alternating tree, and every exposed node of the graph is contained in the forest.
If we have a matchingM and would like to extend it, we take an exposed vertexr and try to match it to a neighbour; but the neighbor may already be in the matching, so we leave this edge out; this creates a new exposed node which we try to match etc. This leads to the notion of alternating paths. Searching for alternating paths from an exposed noderleads to the construction of an alternating tree.
Now we describe the algorithm. We start with the empty matching and the alternating forest consisting of all nodes and no edges. At every stage, either we can increase the size of the matching (deleting some edges but adding more) or we can extend the forest already constructed (and keeping the matching unchanged), or we are stuck and the matching is optimal.
Suppose that we already have a candidate matching M and alternating forest F with respect to M.
u
v
e
e
u
v
Figure 4.2: Extending a bipartite graph matching
p u v p u v
Figure 4.3: Extending an alternating forest
must belong to different trees since the graph is bipartite. Letuandvbe the exposed nodes of the two trees. We have an alternating path fromutov, and switching the matching edges along the path we get a new matchingM0 with one more edge thanM. The forest consisting of all exposed points ofM0 is alternating with respect toM0. (See figure 4.2).
Case 2. Otherwise, if there is an edge connecting an outer nodepof the forest to a vertex
uofV(G)\V(F), this vertexucannot be exposed from the definition of an alternating forest. Hence it is the endpoint of an edge of the matching, sayuv. Vertexvis also inV(G)\V(F) (otherwise uwould have to be in F), and so we can extend F by adding the edges puand
uv. (See figure 4.3).
Case 3. If neither of the cases above applies, the outer points must be connected to inner points only. A simple but crucial counting argument will show that the matching must be
4.5. POLYNOMIAL SPACE 73