Graphs: Representation and Algorithms Terminology : • V = Set of

Graphs: Representation and Algorithms
Terminology :
•
•
•
•
•
V = Set of vertices (or nodes)
|V| = # of vertices or cardinality of V (in
usual terminology |V| = n)
E = Set of edges, where an edge is defined
by two vertices
|E| = # of edges or cardinality of E
A Graph, G is a pair G = (V, E)
Labeled Graphs:
We may give edges and vertices labels.
Edges might also be numerically labeled. For
instance if the vertices represent cities, the
edges might be labeled to represent distances.
Directed Graphs:
•
A directed graph is one in which every
edge (u, v) has a direction, so that (u, v) is
different from (v, u). In an undirected
graph, there is no distinction between (u,
v) and (v, u).
A Directed Graph
Subgraph:
A subgraph of (V, E) is a pair (V', E'), where
V' is a subset of V, and E' consists of all edges
(u, v) of E between two nodes of V'.
Connected Component:
A connected component containing the node
u is the subgraph consisting of all nodes v
that may be reached from u by traversing
zero or more edges. A graph may consist of
one or more connected components.
Neighbor:
The neighbors of v are all u such that u v,
and (u, v) E. In this case we can also say that
u and v are adjacent.
Degree:
The degree of v V is equal to its number of
neighbors.
Path :
A path from v to u is a collection u1, u2, . . . uk,
v such that all (ui, ui+1) E. A simple path is a
path in which each ui is distinct.
Cycle:
A cycle is a path that begins and ends at the
same place. In other words its a path from u
to u. The Subpath not including the terminal
node is simple.
Free Tree:
A free tree is a connected graph without a
cycle.
A Free Tree
Q: How many edges are there in a free tree?
Ans: |E| = |V| -1
Properties of Graphs
2) # of graphs with |V| vertices : 2 [|V| ( |V|-1 )]/2
3) degrees : d1, d2, . . . , d|V|
Note: This shows that the number of
nodes with an odd degree must be even.
Adjacency Matrix Implementation
A |V| × |V| matrix of 0's and 1's.
A 1 represents a connection or an edge.
Storage = |V|²
Adjacency Matrix
For a non-directed graph there will always
be symmetry along the top left to bottom
right diagonal. This diagonal will always
be filled with zero's. These facts simplify
coding.
Coding a Graph
Coding is concerned with storing the
graph in an effecient manner. One might
grab all the bits from the adjacency
matrix and concatenate them to form a
binary string. One could say that the
numerical value of the binary string
(consider as the positive integer) is the
number of the graph. Different graphs
have different numbers. Clearly, |V|² bits
would suffice.
Graph
number= 46.
Coding Graphs
Adjacency List Implementation
The Adjacency list implementation
typically uses less space than the
adjacency matrix implementation.
This method is very good for problems
that involve traversing a graph.
Storage = |V| header cells + 2|E| linked list
cells (since each edge in an undirected
graph is counted by both vertices that it
connects). In a directed graph the 2|E| is
replaced by |E|.
Special Kinds of Graphs
Grid:
The neighbours
of (i, j) are (i ±1,
j) and (i, j ± 1),
therefore it
suffices to store
the pair (i, j).
Complete Graph:
A complete graph
Kn is a graph in
which all possible
edges are present.
The matrix for this
graph would
contain all 1's
therefore there
would be no point
in storing it.
Complete
Bipartite Graph:
A complete
bipartite graph
Km, n is a graph
that has its set of
vertices
positioned into
two subsets, one
of size m and the
other of size n.
All possible edges
from one subset
to the other are
present, but there
are no edges
between the
vertices of the
same subset.
Cycle:
The cycle Cn
consists of n
vertices v1, v2, ...,
vn which are
connected by the
edges (v1, v2), (v2,
v3), ....., (vn-1, vn),
(vn, v1).
Wheel :
The wheel Wn is
constructed from
the Cycle Cn by
adding a new
vertex to the
graph and
connecting it to
each of each of
the already
existing vertices.
Möbius Wheel:
The mobius
wheel Mn (where
n is even) is
constructed from
the cycle Cn by
adding additional
edges between
vertices which
are directly
across from each
other.
A sink is a node with
|V|-1 ingoing edges and
no outgoing edges.
Applications of Graphs
Some of the applications of graphs are :
•
•
•
•
•
•
Mazes (using stacks)
Networks (computer, cities ....)
Maps (any geographic databases )
Graphics : Geometrical Objects
Neighborhood graphs
Voronoi diagrams
Graphics :
Many geometrical objects such as cubes,
polyhedra, and wire frame car models,
may be thought of as graphs. These
graphs are more than just nodes and
edges. Their geometrical structure must
also be taken into account.
This structure
contains three kinds of
objects
•
•
•
vertices
edges
faces
Edges are crucial
since, in a three
dimensional object,
an edge will always
belong to only two
faces and two
nodes. For this
reason it makes
sense to number
the edges. Faces
become linked list
of edges, and each
edge lives in only
two faces.
List of Edges
Face1 Face2 . . .
1
2
-
2
3
-
3
4
5
4
1
-
5
-
6
6
-
7
7
.
3
.
.
.
.
.
.
.
.
.
.
.
.
Faces become linked list of edges, and
each edge lives in only two faces
Neighborhood Graphs
These are graphs for
collection of points in
d-dimensional space.
Such point sets may be
visualized by
connecting close
points. There are
many possible
definitions of
closeness. For
example, one might
draw a circle with two
data points at
diametrically opposite
sides. If the circle
contains no other data
points then the two
points may be
considered "close"
and an edge may be
added between them.
MINIMUM SPANNING TREE
We are given as a starting point a
weighted connected graph(V,E), in which
edges have weights or lengths.
Spanning tree: a free tree on V (thus
having |V|-1 edges that are a subset of|E|).
Minimum Spanning tree: the spanning tree
with minimal total weight, where the
weights of the edges picked are summed to
obtain a total weight.
This is a minimum spanning tree
Applications
- Network design: Design networks that
connect n sites at a minimal total cost.
Clearly, edge weights now represent costs
of connections.
- Visualizing multidimensional data: As
points that are close to each other are
connected, multidimensional data
(vectors) may be connected by an MST
to see how they relate to each other.
Each point on the figure below
represents a high-dimensional vector of
observations or measurements
From such a graphs or others like it,
one may deduce patterns in
evolution. For example, by
removing the longest edge in the
MST, one obtains two groups, also
called clusters. If we remove the k
longest edges, we obtain k+1
clusters. This may be useful for
clustering objects.
- Clustering (Classification, Taxonomy)
Make the objects into vectors,
Find the EMST (Euclidean Minimal
Spanning Tree: nodes V represent a ndimensional vector, edge weights are
Euclidean distances between vectors),
Remove the k longest edges. This
yields the grouping as shown in the
mammal's milk example.
Mammal's Milk
ANIMAL WATER PROTEIN FAT LACTOSE
HORSE
90.1
2.6
1.0
6.9
DONKEY
90.3
1.7
1.4
6.2
MULE
90.0
2.0
1.8
5.5
CAMEL
LLAMA
ZEBRA
87.7
86.5
86.2
3.5
3.9
3.0
3.4
3.2
4.8
4.8
5.6
5.3
SHEEP
BUFFALO
FOX
PIG
82.0
82.1
81.6
82.8
5.6
5.9
6.6
7.1
6.4
7.9
5.9
5.1
4.7
4.7
4.9
3.7
RABBIT
RAT
71.3
72.5
12.3
9.2
13.1
12.6
1.9
3.3
DEER
REINDEER
WHALE
65.9
64.8
64.8
10.4
10.7
11.1
19.7
20.3
21.2
2.6
2.5
1.6
Graph Implementation – Adjacency Matrix
Adjacency Matrix for Weighted Graphs
Graph Implementation – Adjacency List
Depth-First Search (DFS)
dfs(in v:Vertex)
{
mark v as visited
for (each unvisited vertex u adjacent to v)
dfs(u)
}
DFS Algorithm Application
BFS Algorithm Application
Minimum Spanning Tree (MST)
Assume the weight of an edge represents a cost value
What is the cheapest solution (tree) that connects all vertices?
Minimum Spanning Tree (MST)
Prim's Algorithm
start at any vertex (= root)
mark root as visited and add it to the MST
while (there are still unvisited vertices)
{
find the least-cost edge (v,u) from a visited vertex v
to some unvisited vertex u
mark u as visited
add vertex u and edge (v,u) to MST
}
MST Example
MST Example
MST Example
MST Example
root
order of inclusion: a, i, f, g, d, h, c, e,
Kruskal's algorithm.
Step 1. In the graph, the Edge(g, h) is shortest. Either vertex g or
vertex h could be representative. Lets choose vertex g arbitrarily.
Step 2. The edge (c, i) creates the second tree. Choose vertex c as
representative for second tree.
Step 3. Edge (g, f) is the next shortest edge. Add this edge and choose
vertex g as representative.
Step 4. Edge (a, b) creates a third tree.
Step 5. Add edge (c, f).
Step 6. Edge (g, i) is the next cheapest, but if we add this edge a cycle
would be created. Vertex c is the representative of both.
Step 7. Instead, add edge (c, d).
Step 8. If we add edge (h, i), edge(h, i) would make a cycle.
Step 9. Instead of adding edge (h, i) add edge (a, h).
Step 10. Again, if we add edge (b, c), it would create a cycle. Add edge
(d, e) instead to complete the spanning tree. In this spanning tree all
trees joined and vertex c is a sole representative.