0% found this document useful (0 votes)
2 views

Graphs

Uploaded by

karankamble0079
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Graphs

Uploaded by

karankamble0079
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Graphs

Basic
Representation
Terminology

Search
BFS & DFS
Introduction
• A graph G is simply a set V of vertices and a
collection E of pairs of vertices from V, called
edges.
• Some books use different terminology for
graphs and refer to what we call vertices as
nodes and what we call edges as arcs.
• Edges in a graph are either directed or undirected.
• An edge (u,v) is said to be directed from u to v if
the pair (u,v) is ordered, with u preceding v.
• An edge (u,v) is said to be undirected if the pair
(u,v) is not ordered.
• Undirected edges are sometimes denoted with set
notation, as {u,v}, but for simplicity we use the pair
notation (u,v), noting that in the undirected case
(u,v) is the same as (v,u).
• If all the edges in a graph are undirected, then
we say the graph is an undirected graph.

• a directed graph, also called a digraph, is a


graph whose edges are all directed.

• A graph that has both directed and undirected


edges is often called a mixed graph.
vertices
1 2
3

5 4

2
Edges
1
5 3
Directed
Edge 4
• The two vertices joined by an edge are called
the end vertices (or endpoints) of the edge.
• If an edge is directed, its first endpoint is its
origin and the other is the destination of the
edge.
• Two vertices u and v are said to be adjacent if
there is an edge whose end vertices are u and v.
• An edge is said to be incident on a vertex if the
vertex is one of the edge’s endpoints.
• The outgoing edges of a vertex are the directed
edges whose origin is that vertex. The incoming
edges of a vertex are the directed edges whose
destination is that vertex.
adjacent
1 2
3
Incoming
Edges 2
5 4

2
Incident 3
1
5 3
Outgoing
Edges 2 4
• The degree of a vertex v, denoted deg(v), is
the number of incident edges of v. The in-
degree and out-degree of a vertex v are the
number of the incoming and outgoing edges
of v, and are denoted indeg(v) and outdeg(v),
respectively.

2
1
5 3

4
Allowing for two undirected edges to have the same end
vertices, and for two directed edges to have the same origin
and the same destination. Such edges are called parallel
edges or multiple edges.

e3
A e4 B
e5
e2 e1

D C
An edge (undirected or directed) is a self-loop if its
two endpoints coincide

A graphs do not have parallel edges or self-loops such


graphs are said to be simple.

A path is a sequence of alternating vertices and edges


that starts at a vertex and ends at a vertex such that
each edge is incident to its predecessor and successor
vertex.
e2
e3
self-loop A e4 B
e5
e1

D C

A B
simple

D C
Graph Representation

1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List
1. Adjacency Matrix
A B C D
A B A 0 1 0 1
B 1 0 1 1
C 0 1 0 1
D C D 1 1 1 1
1. Adjacency Matrix
A B C D
A B A 0 1 0 1
B 0 0 1 1
C 0 0 0 0
D C D 0 0 1 1
2. Incidence Matrix
e1 e2 e3 e4 e5
A
e1
B A 1 1 0 0 0
e3 e5
B -1 0 1 0 1
e2
C 0 0 0 -1 -1
e4
D C D 0 -1 -1 1 0
2. Adjacency List

A B A B D
B C D
C
D C D C D
2. Adjacency List

A B A B D
B D
C A B D
C D D A
3. Adjacency List
A B

C D

A B D

B D

C A B D

D A
BFS
A B C

D E F

G H I

A D E B G C F H I
Result of BFS

A B C

D E F

G H I
DFS
A B C

D E F I
HE
G H I G
CF
D
B
A

You might also like