Docsity

Graph Theory: Undirected and Directed Graphs, Adjacency Matrices, and Adjacency Lists, Study notes of Computer Science

Toggle sidebar
bg1
CS 373 Lecture 10: Basic Graph Properties Fall 2002
Obie looked at the seein’ eye dog. Then at the twenty-seven 8 by 10 color glossy pictures
with the circles and arrows and a paragraph on the back of each one. . . and then he looked
at the seein’ eye dog. And then at the twenty-seven 8 by 10 color glossy pictures with the
circles and arrows and a paragraph on the back of each one and began to cry.
Because Obie came to the realization that it was a typical case of American blind justice,
and there wasn’t nothin’ he could do about it, and the judge wasn’t gonna look at the
twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on
the back of each one explainin’ what each one was, to be used as evidence against us.
And we was fined fifty dollars and had to pick up the garbage. In the snow.
But that’s not what I’m here to tell you about.
— Arlo Guthrie, “Alice’s Restaurant” (1966)
10 Basic Graph Properties (October 17)
10.1 Definitions
Agraph Gis a pair of sets (V, E). Vis a set of arbitrary objects which we call vertices 1or nodes.
Eis a set of vertex pairs, which we call edges or occasionally arcs. In an undirected graph, the
edges are unordered pairs, or just sets containing two vertices. In a directed graph, the edges are
ordered pairs of vertices. We will only be concerned with simple graphs, where there is no edge
from a vertex to itself and there is at most one edge from any vertex to any other.
Following standard (but admittedly confusing) practice, I’ll also use Vto denote the number of
vertices in a graph, and Eto denote the number of edges. Thus, in an undirected graph, we have
0≤E≤V
2, and in a directed graph, 0 ≤E≤V(V−1).
We usually visualize graphs by looking at an embedding. An embedding of a graph maps each
vertex to a point in the plane and each edge to a curve or straight line segment between the two
vertices. A graph is planar if it has an embedding where no two edges cross. The same graph can
have many different embeddings, so it is important not to confuse a particular embedding with the
graph itself. In particular, planar graphs can have non-planar embeddings!
a
b
e
d
fg
h
i
c
a
b
ed
f
gh
i
c
A non-planar embedding of a planar graph with nine vertices, thirteen edges, and two connected components,
and a planar embedding of the same graph.
There are other ways of visualizing and representing graphs that are sometimes also useful. For
example, the intersection graph of a collection of objects has a node for every object and an edge
for every intersecting pair. Whether a particular graph can be represented as an intersection graph
depends on what kind of object you want to use for the vertices. Different types of objects—line
segments, rectangles, circles, etc.—define different classes of graphs. One particularly useful type
of intersection graph is an interval graph, whose vertices are intervals on the real line, with an edge
between any two intervals that overlap.
1The singular of ‘vertices’ is vertex. The singular of ‘matrices’ is matrix. Unless you’re speaking Italian, there
is no such thing as a vertice, a matrice, an indice, an appendice, a helice, an apice, a vortice, a radice, a simplice, a
directrice, a dominatrice, a Unice, a Kleenice, or Jimi Hendrice!
1
pf3
pf4
pf5

Partial preview of the text

Download Graph Theory: Undirected and Directed Graphs, Adjacency Matrices, and Adjacency Lists and more Study notes Computer Science in PDF only on Docsity!

Obie looked at the seein’ eye dog. Then at the twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one... and then he looked at the seein’ eye dog. And then at the twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one and began to cry. Because Obie came to the realization that it was a typical case of American blind justice, and there wasn’t nothin’ he could do about it, and the judge wasn’t gonna look at the twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one explainin’ what each one was, to be used as evidence against us. And we was fined fifty dollars and had to pick up the garbage. In the snow. But that’s not what I’m here to tell you about. — Arlo Guthrie, “Alice’s Restaurant” (1966)

10 Basic Graph Properties (October 17)

10.1 Definitions

A graph G is a pair of sets (V, E). V is a set of arbitrary objects which we call vertices 1 or nodes. E is a set of vertex pairs, which we call edges or occasionally arcs. In an undirected graph, the edges are unordered pairs, or just sets containing two vertices. In a directed graph, the edges are ordered pairs of vertices. We will only be concerned with simple graphs, where there is no edge from a vertex to itself and there is at most one edge from any vertex to any other. Following standard (but admittedly confusing) practice, I’ll also use V to denote the number of vertices in a graph, and E to denote the number of edges. Thus, in an undirected graph, we have 0 ≤ E ≤

(V

2

, and in a directed graph, 0 ≤ E ≤ V (V − 1). We usually visualize graphs by looking at an embedding. An embedding of a graph maps each vertex to a point in the plane and each edge to a curve or straight line segment between the two vertices. A graph is planar if it has an embedding where no two edges cross. The same graph can have many different embeddings, so it is important not to confuse a particular embedding with the graph itself. In particular, planar graphs can have non-planar embeddings!

a

b

e

d

f g

h

c i

a

b

e d f

h g

i

c

A non-planar embedding of a planar graph with nine vertices, thirteen edges, and two connected components, and a planar embedding of the same graph.

There are other ways of visualizing and representing graphs that are sometimes also useful. For example, the intersection graph of a collection of objects has a node for every object and an edge for every intersecting pair. Whether a particular graph can be represented as an intersection graph depends on what kind of object you want to use for the vertices. Different types of objects—line segments, rectangles, circles, etc.—define different classes of graphs. One particularly useful type of intersection graph is an interval graph, whose vertices are intervals on the real line, with an edge between any two intervals that overlap.

(^1) The singular of ‘vertices’ is vertex. The singular of ‘matrices’ is matrix. Unless you’re speaking Italian, there is no such thing as a vertice, a matrice, an indice, an appendice, a helice, an apice, a vortice, a radice, a simplice, a directrice, a dominatrice, a Unice, a Kleenice, or Jimi Hendrice!

a

b

d e f

c

g h

i

a

b

e

d f g h

c i a b

c d e^

f g^

h i

(a) (b) (c) The example graph is also the intersection graph of (a) a set of line segments, (b) a set of circles, or (c) a set of intervals on the real line (stacked for visibility).

If (u, v) is an edge in an undirected graph, then u is a neighbor or v and vice versa. The degree of a node is the number of neighbors. In directed graphs, we have two kinds of neighbors. If u → v is a directed edge, then u is a predecessor of v and v is a successor of u. The in-degree of a node is the number of predecessors, which is the same as the number of edges going into the node. The out-degree is the number of successors, or the number of edges going out of the node. A graph G′^ = (V ′, E′) is a subgraph of G = (V, E) if V ′^ ⊆ V and E′^ ⊆ E. A path is a sequence of edges, where each successive pair of edges shares a vertex, and all other edges are disjoint. A graph is connected if there is a path from any vertex to any other vertex. A disconnected graph consists of several connected components, which are maximal connected subgraphs. Two vertices are in the same connected component if and only if there is a path between them. A cycle is a path that starts and ends at the same vertex, and has at least one edge. A graph is acyclic if no subgraph is a cycle; acyclic graphs are also called forests. Trees are special graphs that can be defined in several different ways. You can easily prove by induction (hint, hint, hint) that the following definitions are equivalent.

  • A tree is a connected acyclic graph.
  • A tree is a connected component of a forest.
  • A tree is a connected graph with at most V − 1 edges.
  • A tree is a minimal connected graph; removing any edge makes the graph disconnected.
  • A tree is an acyclic graph with at least V − 1 edges.
  • A tree is a maximal acyclic graph; adding an edge between any two vertices creates a cycle.

A spanning tree of a graph G is a subgraph that is a tree and contains every vertex of G. Of course, a graph can only have a spanning tree if it’s connected. A spanning forest of G is a collection of spanning trees, one for each connected component of G.

10.2 Explicit Representations of Graphs

There are two common data structures used to explicitly represent graphs: adjacency matrices 2 and adjacency lists. The adjacency matrix of a graph G is a V × V matrix of indicator variables. Each entry in the matrix indicates whether a particular edge is or is not in the graph:

A[i, j] =

[

(i, j) ∈ E

]

(^2) See footnote 1.

10.3 Traversing connected graphs

Suppose we want to visit every node in a connected graph (represented either explicitly or implic- itly). The simplest method to do this is an algorithm called depth-first search, which can be written either recursively or iteratively. It’s exactly the same algorithm either way; the only difference is that we can actually see the ‘recursion’ stack in the non-recursive version. Both versions are initially passed a source vertex s.

RecursiveDFS(v): if v is unmarked mark v for each edge (v, w) RecursiveDFS(w)

IterativeDFS(s): Push(s) while stack not empty v ← Pop if v is unmarked mark v for each edge (v, w) Push(w)

Depth-first search is one (perhaps the most common) instance of a general family of graph traversal algorithms. The generic graph traversal algorithm stores a set of candidate edges in some data structure that I’ll call a ‘bag’. The only important properties of a ‘bag’ are that we can put stuff into it and then later take stuff back out. (In C++^ terms, think of the ‘bag’ as a template for a real data structure.) Here’s the algorithm:

Traverse(s): put (∅, s) in bag while the bag is not empty take (p, v) from the bag (?) if v is unmarked mark v parent(v) ← p for each edge (v, w) (†) put (v, w) into the bag (??)

Notice that we’re keeping edges in the bag instead of vertices. This is because we want to remember, whenever we visit a vertex v for the first time, which previously-visited vertex p put v into the bag. The vertex p is called the parent of v.

Lemma 1. Traverse(s) marks every vertex in any connected graph exactly once, and the set of edges (v, parent(v)) with parent(v) 6 = ∅ form a spanning tree of the graph.

Proof: First, it should be obvious that no node is marked more than once. Clearly, the algorithm marks s. Let v 6 = s be a vertex, and let s → · · · → u → v be the path from s to v with the minimum number of edges. Since the graph is connected, such a path always exists. (If s and v are neighbors, then u = s, and the path has just one edge.) If the algorithm marks u, then it must put (u, v) into the bag, so it must later take (u, v) out of the bag, at which point v must be marked (if it isn’t already). Thus, by induction on the shortest-path distance from s, the algorithm marks every vertex in the graph. Call an edge (v, parent(v)) with parent(v) 6 = ∅ a parent edge. For any node v, the path of parent edges v → parent(v) → parent(parent(v)) → · · · eventually leads back to s, so the set of

parent edges form a connected graph. Clearly, both endpoints of every parent edge are marked, and the number of parent edges is exactly one less than the number of vertices. Thus, the parent edges form a spanning tree. 

The exact running time of the traversal algorithm depends on how the graph is represented and what data structure is used as the ‘bag’, but we can make a few general observations. Since each vertex is visited at most once, the for loop (†) is executed at most V times. Each edge is put into the bag exactly twice; once as (u, v) and once as (v, u), so line (??) is executed at most 2E times. Finally, since we can’t take more things out of the bag than we put in, line (?) is executed at most 2 E + 1 times.

10.4 Examples

Let’s first assume that the graph is represented by an adjacency list, so that the overhead of the for loop (†) is only a constant per edge.

  • If we implement the ‘bag’ by using a stack, we have depth-first search. Each execution of (?) or (??) takes constant time, so the overall running time is O(V + E). Since the graph is connected, V ≤ E + 1, so we can simplify the running time to O(E). The spanning tree formed by the parent edges is called a depth-first spanning tree. The exact shape of the tree depends on the order in which neighbor edges are pushed onto the stack, but the in general, depth-first spanning trees are long and skinny.
  • If we use a queue instead of a stack, we have breadth-first search. Again, each execution of (?) or (??) takes constant time, so the overall running time is still O(E). In this case, the breadth-first spanning tree formed by the parent edges contains shortest paths from the start vertex s to every other vertex in its connected component. The exact shape of the shortest path tree depends on the order in which neighbor edges are pushed onto the queue, but the in general, shortest path trees are short and bushy. We’ll see shortest paths again next week.

a

b

e

d

f

c

a

b

e

d

f

c A depth-first spanning tree and a breadth-first spanning tree of one component of the example graph, with start vertex a.

  • Suppose the edges of the graph are weighted. If we implement the ‘bag’ using a priority queue, always extracting the minimum-weight edge in line (?), then we we have what might be called shortest-first search. In this case, each execution of (?) or (??) takes O(log E) time, so the overall running time is O(V + E log E), which simplifies to O(E log E) if the graph is connected. For this algorithm, the set of parent edges form the minimum spanning tree of the connected component of s. We’ll see minimum spanning trees again in the next lecture.

If the graph is represented using an adjacency matrix, the finding all the neighbors of each vertex in line (†) takes O(V ) time. Thus, depth- and breadth-first search take O(V 2 ) time overall, and ‘shortest-first search’ takes O(V 2 + E log E) = O(V 2 log V ) time overall.