GRAPHS
Representations
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 26 / 101
Representing Graphs
• A graph G = (V, E) has two natural input parameters, the number
of nodes |V |, and the number of edges |E|. Let n = |V | and m = |E|
respectively.
• Running times will be in terms n (or |V|) and m (or |E|). We aim for
polynomial running times, and lower degree polynomials are better.
• Aim: to implement the basic graph search algorithms in O(m + n).
• Refer to this as linear time, since it takes O(m + n) time simply to
read the input.
• Also, m ≥ n − 1 for connected graphs.
• Two ways to represent graphs:
☞ Adjacency Matrix and
☞ Adjacency list.
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 27 / 101
Adjacency Matrix Representation
• Let G = (V, E) be a graph with n vertices, n >1.
• The adjacency matrix of G is a two-dimensional n × n array
☞ a[i, j] = 1 iff the edge (i, j) ∈ E(G) (< i, j >for a directed graph).
☞ The element a[i, j] =0 if (i, j) /
∈ G.
• Undirected graph: the adjacency matrix is symmetric, i.e., a[u, v] = a[v, u]
for all nodes u, v ∈ V .
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 28 / 101
Adjacency matrix
• Space requirements: O(n2) (Store the upper triangular of the AM).
• Operations on Graphs: To check if (u, v) is an edge takes Θ(1) time.
• Degree of a vertex in undirected graph:
∑n
j=1 a[i, j]
• Degree of a vertex in a directed graph :
☞ Row sum gives the out-degree
∑n
j=1 a[i, j]
☞ Column sum is the in-degree
∑n
j=1 a[j, i].
• How many edges are there in G? or Is G connected ?
Requires (at least) Ω(n2) time.
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 29 / 101
Limitations of Adjacency Matrix (AM)
• The representation takes Θ(n2) space. But when the graph has many
fewer edges than n2, more compact representations are possible.
• Many graph algorithms need to examine all edges incident to a
given node v.
• With AM, doing this involves considering all other nodes w, and
checking the matrix entry A[v, w] to see whether the edge (v, w)
is present and this takes Θ(n) time.
• We would expect that graph connectivity questions could be answered
in significantly less time, say O(m + n), and m < n2
2 .
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 30 / 101
Adjacency List
• n rows of the adjacency matrix are represented as n linked lists. There is one
list for each vertex in G.
• The nodes in list i represent the vertices that are adjacent from vertex i.
• Each node has at least two fields: vertex and link. The vertex field contains
the indices of the vertices adjacent to vertex i.
Figure: Examples
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 31 / 101
Find the adjacency list representation
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 32 / 101
Advantages
• The vertices in each list are not required to be ordered.
• The head nodes are sequential to provide easy random access to the
adjacency list for any particular vertex.
• Undirected graph: It requires n head nodes and 2m list node.
☞ Space for adjacency list representation: O(m + n) space.
• Operations
• Is (u, v) ∈ E(G)? O(degree(u))
• To find degree of any vertex (u) in an undirected graph
☞ Count the number of nodes in its adjacency list
(c + O(degree(u)))
• So,the number of edges in G can be determined in O(m + n) time.
(Memory access time).
(DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 33 / 101

302_Graph_representation_design_and_analysis_of_algorithms.pdf

  • 1.
    GRAPHS Representations (DAA, Dr. AshishGupta) ULC403 : Unit-I/II 26 / 101
  • 2.
    Representing Graphs • Agraph G = (V, E) has two natural input parameters, the number of nodes |V |, and the number of edges |E|. Let n = |V | and m = |E| respectively. • Running times will be in terms n (or |V|) and m (or |E|). We aim for polynomial running times, and lower degree polynomials are better. • Aim: to implement the basic graph search algorithms in O(m + n). • Refer to this as linear time, since it takes O(m + n) time simply to read the input. • Also, m ≥ n − 1 for connected graphs. • Two ways to represent graphs: ☞ Adjacency Matrix and ☞ Adjacency list. (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 27 / 101
  • 3.
    Adjacency Matrix Representation •Let G = (V, E) be a graph with n vertices, n >1. • The adjacency matrix of G is a two-dimensional n × n array ☞ a[i, j] = 1 iff the edge (i, j) ∈ E(G) (< i, j >for a directed graph). ☞ The element a[i, j] =0 if (i, j) / ∈ G. • Undirected graph: the adjacency matrix is symmetric, i.e., a[u, v] = a[v, u] for all nodes u, v ∈ V . (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 28 / 101
  • 4.
    Adjacency matrix • Spacerequirements: O(n2) (Store the upper triangular of the AM). • Operations on Graphs: To check if (u, v) is an edge takes Θ(1) time. • Degree of a vertex in undirected graph: ∑n j=1 a[i, j] • Degree of a vertex in a directed graph : ☞ Row sum gives the out-degree ∑n j=1 a[i, j] ☞ Column sum is the in-degree ∑n j=1 a[j, i]. • How many edges are there in G? or Is G connected ? Requires (at least) Ω(n2) time. (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 29 / 101
  • 5.
    Limitations of AdjacencyMatrix (AM) • The representation takes Θ(n2) space. But when the graph has many fewer edges than n2, more compact representations are possible. • Many graph algorithms need to examine all edges incident to a given node v. • With AM, doing this involves considering all other nodes w, and checking the matrix entry A[v, w] to see whether the edge (v, w) is present and this takes Θ(n) time. • We would expect that graph connectivity questions could be answered in significantly less time, say O(m + n), and m < n2 2 . (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 30 / 101
  • 6.
    Adjacency List • nrows of the adjacency matrix are represented as n linked lists. There is one list for each vertex in G. • The nodes in list i represent the vertices that are adjacent from vertex i. • Each node has at least two fields: vertex and link. The vertex field contains the indices of the vertices adjacent to vertex i. Figure: Examples (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 31 / 101
  • 7.
    Find the adjacencylist representation (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 32 / 101
  • 8.
    Advantages • The verticesin each list are not required to be ordered. • The head nodes are sequential to provide easy random access to the adjacency list for any particular vertex. • Undirected graph: It requires n head nodes and 2m list node. ☞ Space for adjacency list representation: O(m + n) space. • Operations • Is (u, v) ∈ E(G)? O(degree(u)) • To find degree of any vertex (u) in an undirected graph ☞ Count the number of nodes in its adjacency list (c + O(degree(u))) • So,the number of edges in G can be determined in O(m + n) time. (Memory access time). (DAA, Dr. Ashish Gupta) ULC403 : Unit-I/II 33 / 101