H08-F12-CS40 page 1
section
First name
(color-in initial)
A B C D E F
G H I J K L M
N O P Q R S
T U V W X Y Z (M2,M5,M7 or T4)
Last name
(color-in initial)
A B C D E F
G H I J K L M
N O P Q R S
T U V W X Y Z
first name
initial
last name
initial
H08: Due Fri 10.12.2012 in Lecture. Total Points: 50
MAY ONLY BE TURNED IN DURING Lecture ON Fri 10.12.2012, or offered in person, for in person grading, during instructor or TAs office hours.
See the course syllabus at https://foo.cs.ucsb.edu/40wiki/index.php/F12:Syllabus for more details.
(1) (10 pts) Fill in the information below. Also, fill in the A-Z header by
coloring in the first letter of your first and last name (as it would appears in Gauchospace),
writing either M2, M5 or T4 to indicate your discussion section meeting time,
i.e., Monday at 2pm, Monday at 4pm, or Tuesday at 5pm.
writing your first and last initial in large capital letters (e.g. P C).
All of this helps us to manage the avalanche of paper that results from the daily homework.
name:
umail address:
@umail.ucsb.edu
Your reading assignment for H08 includes
Section 1.4 from your textbook (56-73)
The handout given out with this assignment (which can be found on the wiki under the link for Homework H08---it is part of the printable PDF)
Also read Section 1.5, the chapter summary, on p. 74.
(2) (20 pts) For each of the following diagrams, write the formal specification of the graph shown in the manner illustrated on the
handout, i.e. as a tuple containing a set of vertices, and a set of edges. Note that if the lines do not have arrows, the graph is an
undirected graph, while if they do have arrows, the graph is a directed graph. Make note of the difference. In an undirected graph, you
should only specify an edge between nodes a,b once as (a,b); don't also include (b,a).
The first is done for you as an exercise; the rest are worth 4 points each.
G=( {1,2,3,4} , {(1,3),(1,4),(2,3),(2,4)}
F12-H08-page 2
(3) Consider the following graph
(a) (3 pts) What is the largest in-degree in this graph?
(b) (3 pts) What is the set of nodes that has that in-degree? Specify this as a set.
(c) (3 pts) What is the largest out-degree in this graph?
(d) (3 pts) What is the set of nodes that has that out-degree? Specify this as a set.
(4) (4 pts) Specify a minimum coloring of this graph (by putting a color on each node, such that no two edges have the same color.)
(5) (4 pts) What is the chromatic number of the graph below?
H08-F12-CS40 HANDOUT page 1
BRING THIS HANDOUT WITH YOU TO SECTION ON MONDAY 10.15 or TUESDAY 10.16
Terms you need to know from Section 1.4
Review the definitions of the following important terms. You will need to know these terms throughout the rest of the course (and may
need them for certain upper division courses in CS, including but not limited to CS130A, CS130B, CS140, CS160, CS176A and
possibly others.)
vertex
vertices (plural of vertex)
node (synonym for vertex)
edge
adjacent
complete graph
n-colorable
chromatic number
planar
About Graphs
In common speech, the word "graph" can mean many different things. We may think of "graphing a function" by connecting its points
on the Cartesian Plane (x and y axis). Or we can think of graphs such as the "bar charts" and "pie charts" we can make using programs
like Microsoft Excel, or MATLAB. However, when we speak of a graph in CS40, we are talking about something different: a specific
type of data structure that has vertices and edges.
A graph G is defined as a tuple (V,E), which is a tuple containing two items:
V is a set of vertices (sometimes called nodes).
The singular form of vertices is vertex.
E is a set of edges, which is a subset of V×V (the Cartesian product of V with itself).
So every edge is a pair of vertices.
As an example, this graph has a set of vertices representing Airports, and a set of edges representing flights connecting those Airports:
Airports
Friends
Pre-reqs
(I realize that in reality there are many more flights than what is shown in this diagram—this is just an example. Maybe suppose these
are just the flights that on some given day actually have seats available, and then perhaps it is more realistic.)
H08-F12-CS40 HANDOUT page 2
Practical Applications of Graphs
Graphs are useful in many practical contexts. For example, a graph can show any of the following:
what the graph represents vertices (nodes) represent
edge (u,v) represents
Facebook friendships
people
u and v are friends
airline routes
airports
there is a flight from u to v
course pre-requisities
courses
course u is a pre-req of course v
There are many kinds of graphs. Two important kinds are:
undirected graphs
directed graphs
In both cases, the vertices in the graph are represented by labelled points or circles, and the edges are represented by lines connecting
the circles. The difference is that in a undirected graph, the lines have no arrows, and in a directed graph, the lines have arrows showing
which direction the edge goes.
In Facebook, friendship is symmetric—u is a friend of v if and only if v is a friend of u. So, it makes sense to use an undirected graph—
we connect two people with an edge if they are friends, and we don't need any arrows on the edge.
As an example of a directed graph, consider the prerequisite relationships for CS courses: Here the arrows flow from a course x to a
course y if x is a pre-requisite for y. Obviously, if x is a pre-req for y, it can't be the case that y is also a pre-req for x; otherwise it would
be impossible to satisfy the pre-reqs for either course.
Formal Representation of Undirected Graphs
An undirected graph is represented as a tuple G=(V,E), where
V is a set of vertices and E is a set of edges
Each member of E is actually a set with exactly two members, {u,v}, where u and v are both vertices, i.e. u∈V and v∈V, and u≠v.
Instead of the set notation {u,v}, we sometimes use the tuple notation (u,v) to stand for an edge.In the context of an undirected
graph, edges (u,v) and (v,u) are considered the "same edge".
So, even if we use the tuple notation instead of the set notation, we know that when listing the set of edges for an undirected
graph, we don't need list both (u,v) and (v,u) to know that u and v share an edge—in fact, we should not list both as if they were
separate edges.
Each of the following diagrams is a representation of the same graph G, a graph that could be specified formally as:
G=({1,2,3},{(1,3),(1,2)}).
Five equivalent ways to draw the undirected graph G=(V,E)=({1,2,3},{(1,3),(1,2)}).
Figure 1.1 in your textbook shows two representations of the same undirected graph. Note that relocating the vertices doesn't change the
structure of the graph. Neither does putting the vertex numbers inside the circles. The only thing that matters is whether the diagram
shows the correct set of vertices, and the correct set of edges. That is, two graphs are equal if their set of vertices is equal, and their set
of edges is equal.
H08-F12-CS40 HANDOUT page 3
Formal Representation of Directed Graphs
A directed graph is represented as a tuple G=(V,E), where:
V is a set of vertices, and E is a set of edges, like before, but:
E ⊆ V×V. That is, each element of E is a true ordered pair of two vertices (u,v), with the edge going from vertex u to' vertex v.
Therefore, the very basis of a graph is rooted in all the set theory and notations that we covered in Sections 1.2 and 1.3 of the textbook.
Complete Graphs
A complete graph on n nodes is one where every possible edge is present—i.e. every vertex is connected to every other vertex.
A planar graph is one that can be drawn on a plane (e.g. a flat piece of paper, or a chalkboard) in such a way that none of the edges
cross. In a planar graph, edges are allowed to be curved—they don't have to be straight lines—but they can never touch each other.
Here is an illustration of the complete graphs on 1, 2, 3, 4 and 5 nodes, which have the special names K1, K2, K3, K4, K5. Do you see
that K1 through K4 are shown to be planar, but that K5 is not planar? To convince yourself of that, try adding a node to K4 in such a
way that the lines to the other four nodes don't cross any other lines—you'll find that you can't do it.
K1
K2,
K3,
K4,
K5.
H08-F12-CS40 HANDOUT page 4
Definitions: degree, in-degree, out-degree
The degree of a vertex in an undirected graph is the number of edges that touch that vertex.
In a directed graph, we define the in-degree of a vertex as the number of edges that come into a vertex, while the out-degree is the
number of edges that go out of a particular vertex.
Definitions: coloring, chromatic number
A coloring of a graph is a way of assigning elements from a set of colors to each node (e.g. {red, green, blue, ... }, or equivalently
numbers {1, 2, 3, ... } such that no two nodes that are connected by an edge have the same color.
The chromatic number of a graph is the smallest number of colors that can be used to make a coloring of that graph.
Here are examples of coloring of two graphs. For the CS pre-req graph, the chromatic number is 2 (b and r stand for blue and red, i.e.
two colors). For the airport graph, four colors are used, the integers 1 through 4. Can you show that 4 colors are needed, or
alternatively, find a way to color the graph with only 3 colors?
H08-F12-CS40 HANDOUT page 5
Weighted Graphs
We've said so far that edges are a tuple (a,b) which indicates an edge from a to b (in a directed graph), or an edge between a and b (in an
undirected graph.)
We can also associate a number (an integer, or a real number) with each edge, so that each edge becomes a triple (a,b,w), where the w
represents some additional information about each edge. For example:
if our graph represents a road network, the vertices might be cities, and the edge weights might be distances
if our graph represents a computer network, the vertices might be routers, and the edges might be delay
if our graph represents an airline network, the vertices might be airports, and the edges might be one-way fares
Here's an example with some actual fares from Fall of 2011 on a well-known low-cost airline.
More Terms to Know
Know the definition of a subgraph (p. 60)
In the definition of subgraph on p. 60, there is a bit of confusing notation. Earlier in the book, the author uses prime as the
operator for complement (as in the complement of a set.) Here, prime just means that V' is a different set from V. Yes, this is
confusing. I'll send the author an email and ask him to fix this.
Instead of prime, most textbooks use an "overbar" to indicate the complement of a set---for example the complement of A∪B
is A∪B, not (A∪B)'.
Know the definition of a path in a graph (Section 1.4.2)
Know what a cycle is, and what an acyclic graph is.
Know that the definition of a path doesn't prevent a vertex or an edge from appearing more than once.
Know what a connected graph is (p. 61)
Know what weakly connected and strongly connected mean in a directed graph.
Know what an Euler path is (path that contains every edge exactly once)
Know what an Euler circuit is (euler path that starts and ends at the same vertex
H08-F12-CS40 HANDOUT page 6
Section 1.4.4: Trees
Know these terms:
Formal definition of a tree, i.e. "a graph that is connected and has no cycles"
rooted tree vs. free tree
nodes, branches, leaf
terms that typically only apply to rooted trees, including
parent, child
ancestor (note that the root is often considered an "ancestor" of itself, just because it makes certain definitions cleaner.
descendant
height, or depth of a tree
subtree
One note about the definition of depth or height—this definition can vary from author to author.
Our author uses: length of longest path from a root to a node. This means that a tree containing only one vertex is a tree of depth 0.
Some authors call that a tree of depth 1, but in this course, we'll stick with the authors definition except where noted.
Just be clear that you need to be flexible if another book or another professor uses a different definition that is "off by one" from
this author's definition.
© Copyright 2026 Paperzz