14 Graph Intro
14 Graph Intro
ORD
SFO
LAX DFW
Graphs 1
Outline and Reading
Graphs (§6.1)
◼ Definition
◼ Applications
◼ Terminology
◼ Properties
◼ ADT
Data structures for graphs (§6.2)
◼ Edge list structure
◼ Adjacency list structure
◼ Adjacency matrix structure
Graphs 2
Graphs: Motivation (6.1)
Suppose we want to visualize flight routes in a country
A natural representation is with a graph
Nodes – cities
Edges – flights between cities
Edge labels - distance
Graphs 3
Graph Formal Definition
A graph is a pair (V, E), where
◼ V is a set of nodes, called vertices
In this example
V ={a, b, c, d, f}
E ={(a, c), (b, c), (c, f), (b, d), (d, f), (c, d)}
Graphs 4
Edge Types flight
ORD AA 1206 PVD
Directed edge (u,v)
◼ ordered pair of vertices (u,v)
◼ e.g., a flight
Undirected graph
◼ all the edges are undirected
Graphs 6
Applications
cslab1a cslab1b
Paul
David
Graphs 7
Terminology
End vertices (or endpoints)
◼ two vertices joined by an
edge are called the end
vertices of the edge
◼ U and V are the
endpoints of edge a V
An edge is said to be incident a b
h j
on a vertex if the vertex is one
of the edge’s endpoints U d X Z
◼ Edges a, d, and b are
incident on V c e i
Adjacent vertices: 2 vertices W g
are adjacent if there is an
edge between them f
◼ U and V are adjacent
Y
• Degree of a vertex - deg(v):
number of edges incident on v
X has degree 5
Graphs 8
Terminology(Directed graph)
The outgoing edges of a vertex are the
directed edges whose origin is that vertex.
(1,2) (1,3) of vertex 1
Graphs 9
Terminology
Parallel edges
◼ edges that have the same V
endpoints a b
h j
◼ h and i are parallel edges
U d X Z
Self-loop c e i
◼ edge whose endpoints coincide
◼ j is a self-loop
W g
f
Simple graph: Y
◼ If a graph does not have
parallel edges and self loops, it
is said to be a simple graph.
Graphs 10
Terminology
Path
◼ Sequence of alternating
vertices and edges
◼ Begins with a vertex
is a simple cycle f
Y
◼ C2=(U,c,W,e,X,g,Y,f,W,d,V,
Graphs 13
Complete graph
◼ A graph where there exists an edge from every
node to every other node
◼ No. edges: In undirected complete graph it is n(n-
Graphs 14
Subgraph
A subgraph of a graph G is a graph whose vertices
and edges are subsets of the vertices and edges of
G, respectively
Graphs 15
Subgraph
G1 is a subgraph of G2 and G3
Graphs 16
Spanning Subgraph
A spanning subgraph of G is a subgraph of G that
contains all the vertices of the graph G
Graphs 17
Connected components
If a graph G is not connected, its maximal connected
subgraphs are called the connected components of G
Graphs 18
Spanning Tree
A spanning tree of a graph is a connected subgraph of
the graph with all the vertices of the graph and minimum
number of edges possible.
In the context of graph, a tree has no root. So referred as
free tree. The regular tree DS with a root is referred as
rooted tree.
graph tree
Graphs 19
Notation
Properties n
m
number of vertices
number of edges
Property 1 deg(v) degree of vertex v
If G is a graph with m Example
edges, then 2
◼ n = 4
v deg(v) = 2m
◼ m= 6
1 3 ◼ deg(1) = 3
Proof:
◼ deg(2) = 3
• In the above summation
each edge (u,v) is counted ◼ deg(3) = 3
twice 4
◼ deg(4) = 3
• Thus, the total contribution
of the edges to the degrees
of the vertices is twice the
number of edges deg(1) +deg(2) + deg(3) + deg(4) = 12= 2*6
Graphs 20
Notation
n number of vertices
Properties m number of edges
deg(v) degree of vertex v
Property 2
If G is a directed graph with Example
m edges, then 2
◼ n = 4
σ𝒗 ε 𝑮 𝒊𝒏𝒅𝒆𝒈 𝒗 =
◼ m= 6
𝒐𝒖𝒕𝒅𝒆𝒈 𝒗 = 𝒎
1 3 indeg(1) = 1 ; outdeg(1) = 2
𝒗ε𝑮
Proof: indeg(2) = 1 ; outdeg(2) = 2
• In a digraph an edge (u,v) indeg(3) = 2 ; outdeg(3) = 1
4
contributes one unit to indeg(4) = 2 ; outdeg(4) = 1
outdegree and one unit to
indegree indeg(1) +indeg(2) + indeg(3) + indeg(4) = 6
• Thus, the total contribution of outdeg(1) +outdeg(2) + outdeg(3) +
the edges to the outdegree of outdeg(4) = 6
the vertices is equal to the
number of edges and similarly
for indegree Graphs 21
Properties
Property 3
Let G be a simple graph with n vertices and m edges
If G is undirected, then 𝑚 ≤ 𝑛 (𝑛 − 1)/2
If G is directed, then 𝑚 ≤ 𝑛 (𝑛 − 1)
Proof:
◼ Suppose G is undirected the maximum degree of a vertex in G
is n – 1
◼ By property 1 we have 2m ≤ 𝒏 𝒏 − 𝟏
Graphs 22
Operations on Graphs
Accessor methods Update methods
◼ aVertex() ◼ insertVertex(o)
◼ incidentEdges(v) ◼ insertEdge(v, w, o)
◼ endVertices(e) ◼ insertDirectedEdge(v, w, o)
◼ isDirected(e) ◼ removeVertex(v)
◼ origin(e) ◼ removeEdge(e)
◼ destination(e) Generic methods
◼ opposite(v, e) ◼ numVertices()
◼ areAdjacent(v, w) ◼ numEdges()
◼ vertices()
◼ edges()
Graphs 23
Operations on Graphs
Graphs 24
Operations on Graphs
Graphs 25
Operations on Graphs
Graphs 26
Data Structures for
Representing Graphs (6.2)
Adjacency list
Adjacency matrix
For a graph G with n vertices and m edges
◼ adjacency list representation uses O (n + m) space
Graphs 27
Adjacency-list representation
The adjacency-list representation of a graph G =
(V, E) consists of
◼ An array Adj of |V| lists, one for each vertex in V
◼ For each 𝑢 ∈ 𝑉, the adjacency list 𝐴𝑑𝑗[𝑢] contains all the
vertices 𝑣 such that there is an edge 𝑢, 𝑣 ∈ 𝐸
The amount of memory required is O(V + E)
Graphs 28
Example
Graphs 29
Adjacency matrix
The adjacency-matrix representation of a graph G
consists of a |V| x |V| matrix A = aij such that
1 𝑖𝑓 𝑖, 𝑗 ∈ 𝐸
𝑎𝑖𝑗 = ቊ
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
The adjacency matrix of a graph requires O(n2)
memory
|V| ➔Cardinality/No. elements in V
Graphs 30
Example
Graphs 31