Graphs
Graphs
Graphs
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Lesson 22: Basic Graph Concepts<br />
CmSc 175 Discrete Mathematics<br />
1. Introduction<br />
A graph is a mathematical object that is used to model different relations between objects<br />
and processes:<br />
Linked list<br />
Flowchart of a program<br />
Structure chart of a program<br />
Finite state automata<br />
City map<br />
Electric circuits<br />
Course curriculum<br />
Definition: A graph is a collection (nonempty set) of vertices and a set (possibly empty)<br />
of edges<br />
Vertices (also called nodes): can have names and properties<br />
Edges (also called links): connect two vertices, can be labeled, can be directed<br />
Each directed edge has a start vertex and an end vertex<br />
Loop: an edge that connects a vertex to itself<br />
Parallel edges: two or more distinct edges with same start and end.<br />
Example:<br />
Vertices:<br />
Edges:<br />
A,B,C,D<br />
AB, AC, BC, CD<br />
A<br />
B<br />
C<br />
D<br />
1
Same graph given in another way:<br />
C<br />
A<br />
B<br />
D<br />
2. Basic Concepts<br />
The variety of graphs is due to the variety of way we can connect the vertices in a<br />
graph<br />
Loops and parallel edges: e4 and e5 are parallel edges, e6 is a loop<br />
A<br />
e1<br />
B<br />
e3<br />
e6<br />
e2<br />
C<br />
e4<br />
D<br />
e5<br />
Simple graph: a graph without loops and without parallel edges<br />
Adjacent edges: incident on one and the same vertex, e.g. e2 and e3, e2 and e6, etc:<br />
A<br />
e1<br />
B<br />
e2<br />
e3<br />
e6<br />
C<br />
e4<br />
D<br />
e5<br />
2
Isolated vertex: no edges are incident on that vertex<br />
Adjacent vertices: connected by an edge<br />
A path from vertex x to vertex y : a list of vertices in which successive vertices are<br />
connected by edges<br />
Graph1:<br />
A<br />
B<br />
C<br />
D<br />
Some paths in Graph1 are:<br />
A B C D<br />
A C B A C D<br />
A B<br />
D C B<br />
C B A<br />
Simple path:<br />
No vertex is repeated.<br />
Length of a path: the number of edges connecting the vertices in the path<br />
Examples:<br />
C A B is a simple path, while CA B A is not a simple path<br />
Cycle:<br />
Simple path except that the first vertex is equal to the last.<br />
Examples:<br />
C A B C, C B A C, A B C A, A C B A, B A C B, B C A B<br />
3
3. Types of <strong>Graphs</strong><br />
Simple graphs: <strong>Graphs</strong> without loops and without parallel edges<br />
Connected graph:<br />
There is a path between each two vertices<br />
Graph1 is a connected graph.<br />
Examples of disconnected graphs:<br />
Graph2<br />
Graph3<br />
Vertices: A,B,C,D Vertices: A,B,C,D<br />
Edges: AB, CD Edges: AB, AC<br />
A<br />
B<br />
A<br />
B<br />
C<br />
D<br />
C<br />
D<br />
Complete graphs: <strong>Graphs</strong> with all edges present – each vertex is connected to all other<br />
vertices.<br />
Example:<br />
A<br />
B<br />
C<br />
D<br />
E<br />
Dense graphs: relatively few of the possible edges are missing<br />
Sparse graphs: relatively few of the possible edges are present<br />
4
Directed graphs: The edges are oriented, they have a beginning and an end.<br />
Sometimes the edges of a directed graph are called arcs.<br />
Examples:<br />
Graph4:<br />
Graph5:<br />
A<br />
B<br />
A<br />
A<br />
B<br />
A<br />
C<br />
C<br />
D<br />
D<br />
Graph4 and Graph5 are different graphs<br />
Weighted graphs – weights are assigned to each edge (e.g. road map with distances)<br />
Networks: weighted graphs or directed weighted graphs<br />
Examples:<br />
4<br />
A<br />
6<br />
B<br />
C<br />
4<br />
E<br />
9<br />
D<br />
3<br />
4<br />
A<br />
6<br />
B<br />
7<br />
C<br />
4<br />
E<br />
9<br />
D<br />
3<br />
5
4. Subgraphs<br />
Subgraph K of a graph G: a graph with the following properties:<br />
Each vertex in K is a vertex in G.<br />
Each edge in K is an edge in G with the same end-points.<br />
Example:<br />
Graph:<br />
A<br />
B<br />
Subgraphs:<br />
C<br />
D<br />
A<br />
A<br />
B<br />
C<br />
D<br />
D<br />
B<br />
C<br />
D<br />
6
5. Spanning tree of a graph<br />
Tree:<br />
A graph with no cycles<br />
Note: in a tree, when we choose a root we impose an orientation.<br />
We may choose any node to be the root, E.G:<br />
a<br />
b<br />
c<br />
d<br />
e<br />
If we choose vertex a to be the root, then the tree will look like:<br />
a<br />
c<br />
e<br />
b<br />
If we choose c to be the root, then the tree will be:<br />
d<br />
c<br />
a<br />
e<br />
b<br />
d<br />
7
Note also, that the graph does not specify the order of the children of a given node.<br />
A spanning tree of a graph: a subgraph that contains all the vertices, and no<br />
cycles<br />
If we add any edge to the spanning tree, it forms a cycle, and the tree becomes a graph.<br />
Example:<br />
Spanning trees for Graph1:<br />
A<br />
B<br />
A<br />
B<br />
C<br />
D<br />
C<br />
D<br />
A<br />
B<br />
C<br />
D<br />
A tree with N vertices has N-1 edges.<br />
A graph with less than N-1 edges is not connected.<br />
8
6. Graph representation in programming<br />
There are two commonly used methods for graph representation:<br />
Adjacency matrix representation<br />
Adjacency-lists representation<br />
The choice depends on the type of the graph – whether it is dense or not.<br />
1. Adjacency matrix representation.<br />
This method is used when the graph is dense – relatively few of all possible links are<br />
missing.<br />
The representations consists in building a matrix with number of rows equal to the<br />
number of columns, equal to the number of graph nodes.<br />
For the adjacency matrix A the following is true:<br />
A(i,j) = 1 if there node i and node j are connected with an edge.<br />
0 otherwise.<br />
The diagonal elements A(i,i) can be set either to 1 or to 0, whatever is more appropriate<br />
for the particular application.<br />
Example:<br />
Graph G: vertices A,B,C,D,E<br />
Edges: (A,B), (A,D), (B,C),(B,E),(C,D), (C,E)<br />
is represented by the following adjacency matrix:<br />
A B C D E<br />
A 1 1 0 1 0<br />
B 1 1 1 0 1<br />
C 0 1 1 1 1<br />
D 1 0 1 1 0<br />
E 0 1 1 0 1<br />
When implementing this representation it is convenient to assign integers to the vertices.<br />
If the number of nodes is V, the matrix requires V 2 bits of storage and V 2<br />
initialize it.<br />
steps to<br />
2. Adjacency-lists representation.<br />
This representation is used when few of all possible edges are present – i.e. the graph is<br />
not dense. For each node a list is created that holds all nodes, connected to it.<br />
9
For the graph from the above example the adjacency list representation will be the<br />
following:<br />
Graph G: vertices A,B,C,D,E<br />
Edges: (A,B), (A,D), (B,C),(B,E),(C,D), (C,E)<br />
A: B, D<br />
B: A, C, E<br />
C: B, D, E<br />
D: A, C<br />
E: B, C<br />
The adjacency list representation is better for sparse graphs because the space required is<br />
O(V+E), as contrasted to O(V 2 ) for the matrix representation.<br />
7. List of terms<br />
Graph: A collection (nonempty set) V of vertices and E of edges. E may be empty.<br />
Notation G(V, E).<br />
Subgraph: Let G (V, E) is a graph. Gs (Vs, Es) is a subgraph of G iff Vs is a subset of<br />
V, Es is a subset of E, containing those edges in E that connect the vertices of Vs in the<br />
graph G<br />
Loop: An edge that connects a vertex with itself<br />
Parallel edges: distinct edges with same end-points<br />
An edge is incident on each of its end-points<br />
Adjacent edges: incident on one and the same vertex<br />
Adjacent vertices: connected by an edge<br />
Isolated vertex: no edges are incident on that vertex<br />
Path from vertex x to vertex y : a list of vertices starting with x and ending in y in which<br />
successive vertices are connected by edges<br />
Simple path: No vertex is repeated.<br />
Length of a path: the number of edges connecting the vertices in the path<br />
Cycle: A path with no repeated vertices except for the first vertex and the last vertex,<br />
which are the same<br />
Tree unrooted: A graph with no cycles<br />
A spanning tree of a graph: A subgraph that contains all the vertices, and no cycles<br />
10
Types of <strong>Graphs</strong><br />
Simple graphs: <strong>Graphs</strong> without loops and without parallel edges<br />
Complete graphs: <strong>Graphs</strong> with all edges present – each vertex is connected to all other<br />
vertices.<br />
Weighted graphs – weights are assigned to each edge (e.g. road map with distances)<br />
Directed graphs: The edges are oriented, they have a beginning and an end.<br />
Sometimes the edges of a directed graph are called arcs.<br />
Connected graph:<br />
Undirected:<br />
Directed:<br />
There is a path between every two vertices.<br />
There is a path between every two vertices in the underlying undirected graph<br />
Disconnected graph: A graph that is not connected<br />
A graph with N vertices and less than N-1 edges is disconnected.<br />
DAGs – Directed Acyclic <strong>Graphs</strong>, directed graphs without cycles<br />
Networks: Directed or undirected weighted graphs<br />
11