Definitio
ns
2
• Graph: Adata structure that consists of a set of models and a
set of edges that relate the nodes to each other
• Vertex: A node in a graph
• Edge (arc): A pair of vertices representing a connection between
two nodes in a graph
• Undirected graph: A graph in which the edges have no direction
• Directed graph (digraph): A graph in which each edge is
directed from one vertex to another (or the same) vertex
3.
Formall
y
3
A graph Gis defined as follows:
G = (V,E)
where
V(G) is a finite, nonempty set of vertices
E(G) is a set of edges (written as pairs of
vertices)
4.
4
Graph
terminology
• The sizeof a graph is the number of nodes in it
• The empty graph has size zero (no nodes)
• The degree of a node is the number of edges it has
• The in-degree of a node is the number of in-edges it has
• The out-degree of a node is the number of out-edges it has
• For directed graphs
• If a directed edge goes from node S to node D, we call S the source and D the
destination of the edge
• The edge is an out-edge of S and an in-edge of D
• S is a predecessor of D, and D is a successor of S
5.
Graph
Terminology
What are thein-degrees and out-degrees of the vertices a, b, c, d in
this graph
a
b
c
d
deg-(a) = 1
deg+(a) = 2
deg-(b) = 4
deg+(b) = 2
deg-(d) = 2
deg+(d) = 1
deg-(c) = 0
deg+(c) = 2
5
6.
• Example: Whatare the degrees of the vertices in the
graphs G and H?
Solutio
n:
deg(d)
5
deg(a) 4
deg(b) deg(e)
6
In H
deg(c)
1
deg(g)
0
deg(e)
3
deg(b) deg(c) deg(f)
4
deg(a)
2
In G deg(d)
1
a f e g
d
G H
6
e d
b c a b c
7.
Graph
Terminology
Find the in-degreeand the out-
degree of each vertex in the
graph G
Solution: The degree of G are:
deg-(a) =
2
deg-(b) =
2
deg-(c) =
3
deg-(d) =
2
deg-(e) =
deg+(a) =
4
deg+(b) =
1
deg+(c) =
2
deg+(d) =
2
deg+(e) =
a
7
e f
c
b
1. A vertex of degree 0 is called isolated, since
it is not adjacent to any vertex.
2. A vertex with a loop at it has at least degree
2 and, by definition, is not isolated, even if
it is not adjacent to any other vertex.
3. A vertex of degree 1 is
called pendant. It is
adjacent to exactly one other vertex.
8.
8
Graph
terminology
• A cycleis a path whose first and last nodes are
the same
• Example: (London, Bristol, Birmingham, London, Dover) is a
path
• Example: (London, Bristol, Birmingham, London) is a cycle
• A cyclic graph contains at least one cycle
• An acyclic graph does not contain any
cycles
Birmingham Rugby
London
Cambridge
Bristol
Southhampton
Dover
60
140
190
150
190 100
120
110
More definitions :
Cycle
Simplepath with distinct edges, except that the first
vertex is equal to the last
A B C A
B A C
B C B
A C
A B
C
D
A graph without cycles is called acyclic
graph.
14
A spanning treeof an undirected
graph
A sub-graph that contains all the vertices, and no cycles. If we add any
edge to the spanning tree, it forms a cycle, and the tree becomes a graph
A B
C
D
graph
A B
C
D
spanning tree
18
More
Definitions
20
• Adjacent vertices:Two vertices in a graph that are connected
by an edge
• Path: A sequence of vertices that connects two nodes in a
graph
• Complete graph: A graph in which every vertex is directly
connected to every other vertex
• Weighted graph: A graph in which each edge carries a value
Terminolo
gy
• A directedgraph that has no cyclic paths is called a DAG (a Directed
Acyclic Graph).
23
24.
Terminolo
gy
24
• An undirectedgraph is connected if a path exists from every
vertex to every other vertex
• A directed graph is strongly connected if a path exists from
every vertex to every other vertex
• A directed graph is weakly connected if a path exists from
every vertex to every other vertex, disregarding the direction
of the edge
Adjacency matrix –undirected
graphs
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 0 0
Vertices:A,B,C,D
Edges: AC, AB, AD, BD
The matrix is symmetrical
A B
C
D
29
30.
Adjacency matrix –directed
graphs
Vertices:
Edges:
A,B,C,D
AC, AB, BD, DA
A B C D
A 0 1 1 0
B 0 0 0 1
C 0 0 0 0
D 1 0 0 0
A B
C
D
30
Adjacency lists –directed
graphs
Vertices:
Edges:
A,B,C,D
AC, AB, BD, DA
-
Heads lists
A. B C
B. D
C
D
A
A B
C
D
32
33.
Spanning
Tree
A spanning treeis a subset of
Graph G, which has all the
vertices covered with minimum
possible number of edges.
Hence, a spanning tree does not
have cycles and it cannot be
disconnected.
33
34.
Minimum Spanning
Tree
A minimumspanning tree
(MST) of an edge-weighted
graph is a spanning tree
whose weight (the sum of the
weights of its edges) is no
larger than the weight of any
other spanning tree.
34
35.
Minimum Spanning
Tree
MST isa tree because it is acyclic.
MST is spanning
because it covers
every edge.
MST is minimumfor the
obvious reason.
35
GRAPH TRAVERSAL
BFS (BreadthFirst Search) It is a tree traversal algorithm that
−
is also known as Level Order Tree Traversal. In this traversal we will
traverse the tree row by row i.e. 1st row, then 2nd row, and so
on.
DFS (Depth First Search ) It is a tree traversal algorithm that
−
traverses the structure to its deepest node. There are three most
used methods that are used to traverse the tree using DFS. it
goes into depth of each node as the root node and then goes to
the next one.