EECS 805
9/4/00
Notes on Graphs
A graph G consists of two sets
—V a set of vertices or nodes, and E a set of edges each of which connects two elements of V.
(Put picture from the notes here.)
This is usually referred to as a simple graph.
Two vertices are adjacent if they are connected by an edge. To describe the relationship between a vertex and an edge, we say that an edge is incident on a vertex.
In a graph the degree of a vertex is the number of edges incident on it. (Refer to the example above.) Properties —the sum of the degrees of all the vertices is twice the number of edges.
(Rationale:
In general we assume that there is no direction associated with an edge in a graph. Thus, we use the notation (A, B) to indicate there is an edge between A and B. In many applications such as network flow, we do wish to make an edge directed. We do this in the diagram by using an arrow on the edge. Think about airline flights, one way streets, etc.
(Put directed graph pcture here.)
In this case, we'll use the notation
a, b
to indicate that the edge goes from a to b. The indegree of a node is the number of edges entering it and the outdegree is the number of edges leaving it.
If we're modeling a transportation system for example, there are often two or more routes between a pair of cities. In this case we can use a multigraph . In general, a multigraph is just a graph such that there are two or more edges between at least one pair of vertices.
The edges of a graph may also have a weight or cost associated with them. For example, on a map, the "weight" might be the distance between the two cities or the amount of time needed to go from one city to the next.
Special kinds of graphs and notation for them: regular —A graph is regular if all of its vertices have the same degree.
In a complete graph there is an edge between every pair of vertices. Notation: K n
In a cycle
, each vertex has degree two and vertices can be ordered v1v2…vn such that vi is adjacent to vi+1 and v1 is adjacent to vn. Notation: Cn
The wheel Wn is obtained by taking the cycle Cn and adding a new vertex x such that x is connected to each vertex on the cycle.
n-cube —the n-dimensional cube. Notation: Q n
. Q n
is obtained by taking two copies of Q n-1 and connecting the corresponding vertices. If the vertices are labeled with bit strings such that one copy of Q n-1
has a 1 placed in front of the labels of its vertices, and the other copy has a 0 placed in front of its labels, the resulting cube has the property that two vertices are connected to each other if and only if their labels differ in exactly one bit.
A graph is bipartite iff its vertex set V can be divided into two disjoint subsets V
1
and V
2
such that every edge in E connects a vertex in V
1
with a vertex in V
2
A complete bipartite graph is one in which every vertex in V
1
is connected to every vertex in
V
2
. Notation K i,j
where |V
1
| = i and |V
2
| = j.
A star graph is a complete bipartite graph in which |V
1
| = 1. For example, K
1,5
has a central vertex of degree 5 connected to 5 vertices each of degree 1.
Many of the graphs mentioned above are used in models for data communications and parallel processing. In this case, each node represents a computer and each edge represents a direct connection between computers.
A graph is planar iff it can be drawn in the plane in such a way that no two edges cross except at their endpoints.
A graph is k-colorable , if at most k colors are needed to color its vertices so that no two adjacent vertices receive the same color.
Note that all bipartite graphs are 2-colorable. K n
, the complete graph on n vertices requires n colors. subgraph —If G = (V, E) is a graph, then H = (V’, E’) is a subgraph of G iff V’ V, E’
E and every edge in E’ connects two vertices of V’.
Representations of graphs
The three most common ways to represent a graph are to
1) Use pair notation
2) Use an adjacency matrix or
3) Use an adjacency list.
Consider the graph below:
It can be represented by an adjacency matrix as follows:
A B C D E F
A 0 1 0 0 0 1
B 1 0 1 1 1 1
C 0 1 0 1 1 0
D 0 1 0 1 0 0
E 0 1 1 0 0 0
F 1 1 0 0 0 0
And its adjacency list representation is:
A: B, F
B: A, C, D, E, F
C: B, D, E
D: B, C
E: B, C
F: A, B
Paths and Cycles
NOTE: The following definitions differ from what is shown in the book.
A path is a sequence of sequence of vertices v
1
, v
2
, …, v n
such that v i
is adjacent to v i+1
for all i, 1
i < n. For example, in the graph above, ABCDBAFBC is a path. Note that in a path, an edge may be traversed more than once. The length of a path is the number of edges in it.
(The text calls this a walk.)
A simple path is a path in which no edge appears more than once. For example, ABCDBEC is a simple path. Note that vertices may however be repeated. (The text calls this a trail.)
Most of the time we’ll restrict ourselves to simple paths.
A cycle or circuit is a path in which the beginning and ending vertices are the same. A graph with no cycles is called acyclic .
A path that includes all edges of the graph is called an Euler path. An Euler circuit is any cycle that begins and ends at the same vertex and contains every edge of the graph. A
Hamiltonian path passes through each vertex once, and a Hamiltonian cycle passes through each vertex once and returns to the starting vertex.
A graph contains an Euler path iff it has exactly two vertices of odd degree. (Start at one odd degree vertex, travel over the edges in the graph and finish at the other odd degree vertex.) connectivity
—a graph is connected if there is a path from any vertex to any other vertex.
From now on, we’ll assume the graphs we’re dealing with are connected.
A graph traversal is a systematic way to visit or search all of the nodes in a graph. The two methods for doing this in general graphs are the breadth-first and depth-first searches. In each case, the search starts at an arbitrary vertex, v. A breadth-first search begins by visiting all vertices adjacent to v, then all vertices adjacent to v’s neighbors, etc. Whenever a vertex that has already been visited is encountered, it is skipped. The search continues until every vertex in the graph has been visited. In a depthfirst search, we move “forward” as far as possible from v until a previously visited vertex is encountered. In this case, the search backs up and tries moving along a different path. Again, when all vertices have been encountered, the search stops. Let’s look at these searches applied to the graph above.
A breadth-first search beginning at C visits the nodes in the following order: C, D, E, B, A, F.
How the search proceeds: After visiting the neighbors of C, i.e
.D, E, and B, we next look at those vertices which can be reached from C by a path of length 2. The paths CDB and CEB end at a vertex that has already been visited. However, vertices A and F can be reached by going through B so they are added to the list of visited nodes. At this point, all nodes have been visited and the search ends.
A breadth-first search beginning at D for instance would be D, B, C, E, A, F. (Note: there are several other orders that can be obtained beginning at D.)
Now, let’s look at a depth-first search beginning at C. Going forward as far as possible, vertices B, A, and F would be visited. Since there is no unexplored vertex adjacent to F, we back up to A, again encountering no unexplored vertex. When we back up to B, we visit E, back up again, and finally visit D. Thus the final order is C, B, A, F, E, D. Again, this order is not unique.
Note: we’re skipping the algorithms in the book for now.
Trees
A tree is an acyclic, connected graph. There is an implicit order on the nodes of a tree
—the uppermost node is called the root, and the nodes are arranged on different levels representing the distance from the root.
Suppose v and w are adjacent nodes on consecutive levels of a tree with v being “above” w.
Then we say that v is the parent of w and that w is the child of v. A node with no children is called a leaf . The height of a tree is the length of the longest path from the root to a leaf. The height of a node is the longest path from that node to a leaf. The depth of a node is the distance of that node from the root of the tree.
A subtree of a tree T is obtained by taking a node x
T together with all of its descendants. x is the root of the subtree.
A spanning tree for a connected graph is a subgraph that is a tree and contains all of the nodes of the graph.
A tree is ordered if there is a unique ordering of the children of each node. One example of an ordered tree is an expression tree. For example, consider the algebraic expression x - y*z.
By precedence we know that the multiplication should be performed first and then the subtraction. The expression tree is built up from the leaf level and the order of the operands in the original expression is preserved. Otherwise, expressions containing noncommutative operators like
– and would be calculated incorrectly. Here’s the tree corresponding to this expression:
(Skip the representation of a tree as a data object for now.)
Observations: every tree is bipartite and thus is 2-colorable. Every tree is planar.
Binary Trees
A binary tree is a tree in which each node has at most two children. The expression tree above is a binary tree since the operators we were using are binary operators
A binary search tree is an ordered binary tree in which the nodes satisfy the property that every element in the left subtree of a node precedes the element in the node and every node in the right subtree follows the element in the node. For example,
A complete binary tree is one in which each interior (nonleaf) node has two children and all leaves are at the same level of the tree. For example, the tree below is a complete binary tree of height 3.
Counting the nodes in a complete n-ary tree
The root is the only node at level 0. Thus level 0 has m 0 = 1 node.
Since the tree is complete, level one contains m = m 1 nodes.
Since each level one node has m children there are m 2 nodes on this level.
Then, the total number of nodes is 1 + m + m 2 + … + m h where h is the height of the tree.
This is a geometric series whose sum is m h+1 - 1
m - 1
Note: discussion of the following topics is postponed until later. breadth-first and depth-first search algorithms minimal spanning tree algorithms (Prim and Kruskal) inorder, preorder and postorder traversals of trees graph isomorphism