5 Connectivity when direction does not matter
This chapter reads graphs as undirected: their weakly connected components, the edges and nodes whose removal splits a component, their biconnected components, and whether they are bipartite, trees or forests.
Some questions ignore the direction of the edges, for example: whether a path
connects two areas of a program, which single edge or node splits a network when it is removed, or
whether a structure is a tree. For them, an edge a -> b simply
connects a and b.
Problems in this chapter
Section titled “Problems in this chapter”- 5.1 Find the components of a graph read as undirectedYou get the weakly connected components of a graph: the groups of nodes that paths connect when the direction of the edges does not matter.
- 5.2 Find the edges and nodes whose removal splits a graphYou find the bridges and articulation points: the edges and nodes whose removal splits a component of a graph read as undirected.
- 5.3 Divide the edges of a graph into biconnected componentsYou get the biconnected components of a graph read as undirected, also called blocks: groups of edges in which any two edges lie on a common cycle.
- 5.4 Find out whether a graph is bipartiteYou find out whether the nodes of a graph read as undirected split into two sets so that every edge runs from one set to the other.
- 5.5 Find out whether an undirected graph is a tree or a forestYou find out whether a graph read as undirected has no cycles apart from self-loops, and whether it is connected as well.
The examples of this chapter use the calls between the methods of a small program, the dependencies between its packages, and a family tree that records one parent per person.
Reading a graph as undirected
Section titled “Reading a graph as undirected”A graph in Graphity is always directed. The methods of this chapter read it as
undirected: an edge connects its
source and its target, whether or not the reverse edge exists. The
edges a -> b and b -> a count as one undirected edge. So an undirected graph can be stored with
one edge per pair of neighbors or with both directions, and these methods
read it the same way. The pictures of this chapter draw the edges without arrows.
This picture shows the calls between the methods of a small program. The orange edges lie on a cycle of the undirected reading. Such a cycle uses no edge twice, so going along an edge and back is no cycle.
- Call on a cycle
Read like this, parseArgs and run are connected through main, which calls both. And run
calls save and show, which both call format. Read as undirected, these four calls form a
cycle, although no call leads back to run. parse and format call themselves, and each of these
self-loops is a cycle of its own.
Components
Section titled “Components”In the undirected reading, a group of nodes that are connected by paths, as large as it can be, is
called a weakly connected component or a component for
short. Nodes in different components are not connected at all. The calls above are one component.
Without the calls that main and run make, five components remain.
Bridges, articulation points and blocks
Section titled “Bridges, articulation points and blocks”An edge whose removal increases the number of components is called a
bridge. A node whose removal does the same is called
an articulation point. An edge is a bridge exactly if it lies on no cycle. In the calls above, every
call between two different methods is a bridge, except the four orange ones. run is an
articulation point, since without it main and parseArgs are no longer connected to the rest.
The biconnected components of a graph are groups of edges, also called blocks. Any two edges of a block lie on a common cycle, and each block is as large as it can be. Each bridge is a block of its own, and each articulation point belongs to more than one block. The four orange calls between different methods are one block.
Bipartite graphs, trees and forests
Section titled “Bipartite graphs, trees and forests”A graph is bipartite if its nodes split into two sets so that every edge
runs from one set to the other. That is the case exactly if no cycle has an odd number of edges.
Read as undirected, a forest is a graph without cycles, and a tree is a forest that is connected.
The calls above are no forest, because of the cycle through run, save, format and show. The
family tree is a tree in either reading, and every tree is bipartite. See the chapter
Trees and hierarchies for trees whose edges point away from their
root.