Assignment - 03 - DSA
Assignment - 03 - DSA
Assignment – 03
CC-211: Data Structures & Algorithms
Date Announced: 21-May-24 Due Date: 27-May-2024 Total Marks: 2 + 2 = 4 marks
Instructions: Print this document and solve, then submit this to your teacher after solving it.
Name of Student: _______________________________________ Reg. #: __________________________
Draw any object below and color it. (0.5 extra marks)
Department: Department of Computing Program: BS-________ Section: ____________
Introduction
Graphs, as versatile data structures, find applications in various domains. Representing graphs efficiently
is crucial for algorithmic operations. This chapter delves into diverse methods of representing undirected
(Figure a of Page 1) and directed graphs, exploring their intricacies and applicability.
The adjacency matrix, a simple yet intuitive representation, is ideal for dense graphs. In this matrix,
matrix[i][j] signifies whether there's an edge between vertices i and j. For an undirected graph, the
matrix is symmetric. (Figure c of Page 1)
Example: Consider a social network where users are represented as vertices. If there's an edge between
users A and B, matrix[A][B] and matrix[B][A] would both be 1.
In contrast, the adjacency list caters to sparse graphs, offering space efficiency. Each vertex maintains a
list of its neighbors. It is usually defined as an array of linked lists. (Figure b of Page 1)
Example: Imagine a webpage structure where pages are vertices. The adjacency list of a page includes
all pages linked from it, making it easy to navigate through related content.
Page 1 of 9
Department: Department of Computing Program: BS-________ Section: ____________
For directed graphs (Figure a of Page 2), the adjacency matrix remains applicable. Now, matrix[i][j]
denotes whether there's an edge from vertex i to vertex j. (Figure c of Page 2)
The adjacency list for directed graphs maintains outgoing edges for each vertex, providing a clear
representation of directional relationships. (Figure b of Page 2)
Example: In a directed social network, the adjacency list of a user includes users they are following,
illustrating the direction of connections.
3. Other Representations
In an incidence matrix, rows represent vertices, and columns represent edges. The entry matrix[i][j]
indicates if vertex i is incident to edge j. (Figure e of Page 3)
Page 2 of 9
Department: Department of Computing Program: BS-________ Section: ____________
Example: In a flight network, each row corresponds to an airport, and each column to a flight. If airport
A is connected to flight F, matrix[A][F] is 1.
In an Adjacency List, each vertex maintains a list of its neighbors. It is usually defined as an array of linked
lists (Figure b of Page 1). But it can also be defined as linked list of linked lists (Figure c of Page 3) or as
an array of arrays (Figure b of Page 3).
In an incidence matrix, rows represent vertices, and columns represent edges. The entry matrix[i][j]
indicates if vertex i is incident to edge j.
Page 3 of 9
Department: Department of Computing Program: BS-________ Section: ____________
An Adjacency Array is another way to represent the graph. The disadvantage of this approach is that it
is not easy to insert new edges. The two approaches to implement this are depicted in the below
diagrams. In the first approach, we need a separate array to record the indices where each sub-list starts.
In the second approach, we indicate the end of a sub-list by inserting a special value at its end.
3 4 4 1 4 1 2 3
An edge list directly lists all edges, providing simplicity but potentially at the cost of efficiency.
Example: For a friendship graph, the edge list could be [(A, B), (C, D)], denoting friendships between A
and B, and C and D.
Page 4 of 9
Department: Department of Computing Program: BS-________ Section: ____________
In modern languages, using dictionaries for graphs is convenient. Each vertex is a key, and its value is a
list of neighboring vertices. This is actually an implementation of Adjacency List. See figure below.
Example: In a project management tool, tasks can be vertices, and dependencies are the neighboring
vertices in the graph dictionary.
4. Special Considerations
For scenarios where edges have weights, each representation can be extended to accommodate these
weights, enhancing the model's accuracy in real-world situations. See figures below.
Example: In a navigation system, an edge between two locations has a weight representing the distance
or time it takes to travel.
Page 5 of 9
Department: Department of Computing Program: BS-________ Section: ____________
Extending the concept of weighted graphs, negative weights can be assigned to edges. This scenario is
common in financial models where debts or costs are represented.
Example: In a financial network, an edge with a negative weight could signify a financial gain or loss.
Graphs can also have loops (edges from a vertex to itself) and multiple edges between the same pair of
vertices, creating multigraphs.
Example: In a network monitoring system, a device might have a loop indicating a self-check mechanism,
and multiple edges might signify diverse communication channels.
Conclusion
The choice of a graph representation is nuanced, considering factors like graph density, memory
constraints, and the nature of operations to be performed. By understanding the intricacies of each
representation and their applicability to different scenarios, practitioners can design efficient algorithms
and data structures for a wide array of problems.
Page 6 of 9
Department: Department of Computing Program: BS-________ Section: ____________
Represent / illustrate the following four graphs using all of the discussed methods.
Page 7 of 9
Department: Department of Computing Program: BS-________ Section: ____________
Firstly, draw a suitable graph for each of the given scenario; then represent it by using all of the graph
representations.
Scenario:
In a social network, consider a group of friends. Each friend has a different level of influence over the
others by posting his/her videos and number of likes on a post indicate the level of influence attained.
A person may dislike any content too. You can like or dislike a content multiple time. The interactions
are as follows:
• Ali influences Subhan. Subhan influences Basit. Basit influences Ali. Ali influences Sara. Sara
influences Subhan.
• Subhan has total 5 posts; Ali has 3; Basit has 2 and Sara has 1. There is also a new friend in the
circle named Khalid who hasn’t posted anything yet.
• Sara has liked 2 of Ali’s posts twice. Subhan likes 1 post of Sara thrice but he has given 2 dislikes
to one of Ali’s posts. He has seen and liked only 1 post of Ali once.
• Basit has watched 2 posts of Subhan but he is not interested in giving likes or dislikes.
• Ali dislikes whatever he sees from Basit and never sees his posts twice.
Page 8 of 9
Department: Department of Computing Program: BS-________ Section: ____________
Page 9 of 9