0% found this document useful (0 votes)
15 views15 pages

Data Structure 27+28

The document covers data structures focusing on adjacency matrices and lists for graph representation, detailing their implementations and memory management techniques. It explains memory allocation types, including dynamic and static, as well as garbage collection processes like mark-and-sweep and reference counting. The content also addresses common FAQs regarding memory allocation and the purpose of data structures.

Uploaded by

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

Data Structure 27+28

The document covers data structures focusing on adjacency matrices and lists for graph representation, detailing their implementations and memory management techniques. It explains memory allocation types, including dynamic and static, as well as garbage collection processes like mark-and-sweep and reference counting. The content also addresses common FAQs regarding memory allocation and the purpose of data structures.

Uploaded by

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

DATA STRUCTURE

Lecture : 27 + 28
Instructor: Tayyba Khalid
Content:

 Adjacency matrix and adjacency list


 Implementations
 Memory management
 Garbage collection.
Adjacency matrix and adjacency list

■ Adjacency Matrix Representation


An adjacency matrix is a way of representing a graph as a matrix of
boolean (0’s and 1’s)
Let’s assume there are n vertices in the graph So, create a 2D matrix
adjMat[n][n] having dimension n x n.

– If there is an edge from vertex i to j, mark adjMat[i][j] as 1.


– If there is no edge from vertex i to j, mark adjMat[i][j] as 0.
Representation of Undirected Graph
as Adjacency Matrix:

■ The below figure shows an undirected graph. Initially, the entire


Matrix is ​initialized to 0. If there is an edge from source to destination,
we insert 1 because we can go either way.
Representation of Directed Graph as
Adjacency Matrix:

■ The below figure shows a directed graph. Initially, the entire Matrix is ​
initialized to 0. If there is an edge from source to destination, we
insert 1 for that particular adjMat[destination].
implementation

■ IN CLASS
Adjacency List Representation

■ An array of Lists is used to store edges between two vertices. The size of array
is equal to the number of vertices (i.e, n). Each index in this array represents a
specific vertex in the graph. The entry at the index i of the array contains a
linked list containing the vertices that are adjacent to vertex i.

■ Let’s assume there are n vertices in the graph So, create an array of list of size
n as adjList[n].

– adjList[0] will have all the nodes which are connected (neighbour) to
vertex 0.
– adjList[1] will have all the nodes which are connected (neighbour) to
vertex 1 and so on.
Representation of Undirected Graph as Adjacency list:

■ The below undirected graph has 3 vertices. So, an array of list will be
created of size 3, where each indices represent the vertices.
■ Now, vertex 0 has two neighbours (i.e, 1 and 2). So, insert vertex 1
and 2 at indices 0 of array.
■ Similarly, For vertex 1, it has two neighbour (i.e, 2 and 0) So, insert
vertices 2 and 0 at indices 1 of array.
■ Similarly, for vertex 2, insert its neighbours in array of list.
Representation of Directed Graph as
Adjacency list:

■ The below directed graph has 3 vertices. So, an array of list will be
created of size 3, where each indices represent the vertices.
■ Now, vertex 0 has no neighbors. For vertex 1, it has two neighbor (i.e,
0 and 2) So, insert vertices 0 and 2 at indices 1 of array.
■ Similarly, for vertex 2, insert its neighbors in array of list.
Activity:
Memory Management in Data
Structures:
■ Memory management refers to the process of efficiently allocating
and deallocating memory in a program, particularly when dealing with
complex data structures like arrays, linked lists, trees, and graphs.
• Dynamic Memory Allocation: This is when memory is allocated
during runtime, such as when using pointers in languages like C/C++
or objects in languages like Java.
• In this case, memory can be dynamically managed for structures that
grow or shrink in size (e.g., a linked list, where nodes are dynamically
allocated and deallocated).
• Static Memory Allocation: Memory allocation done during compile
time, for fixed-size data structures (e.g., an array with a
predetermined size).
Memory Management
Techniques:
■ Stack Memory:
A type of memory used for automatic storage management. Local variables are
stored in the stack, and once they go out of scope, they are automatically
deallocated.
■ Heap Memory:
A pool of memory used for dynamic allocation. Data structures like linked lists or
trees are allocated here, and manual memory management is required (e.g.,
using malloc in C or new in C++).

Good memory management ensures the efficient use of memory and avoids
issues like memory leaks (unused memory that is not deallocated) or memory
fragmentation (when memory is used inefficiently).
Garbage Collection in Data
Structures:
■ Garbage Collection in data structures is like a smart cleaner on your
computer. It automatically finds and removes old, unused stuff from memory,
making space for new things. This helps keep your computer running
smoothly and prevents it from getting messed up with unnecessary data.
■ There are Two Important data structure used in Garbage Collection are –
■ Heaps
■ Queues
Garbage Collection Process:
• Mark-and-Sweep: The system marks all the objects that are still in
use and then sweeps through memory to remove objects that are no
longer referenced by any part of the program.
• Reference Counting: Each object in memory has a counter that
tracks how many references point to it. When the reference count
drops to zero, the object is eligible for garbage collection.
• Generational Garbage Collection: Objects are divided into
generations based on their age. Younger objects are collected more
frequently than older ones since they tend to become unreachable
more quickly.
FAQ
Q1: What is memory allocation and garbage collection?
Memory allocation is like giving a space to allocate memory. When you need to store data or
variables in a program, the computer sets aside a specific area in its memory for that purpose.
So basically, it is the process of automatically finding and removing unused data from memory
to keep the system running smoothly.

Q2: Which method is called garbage collection?


Garbage collection is the method called to clean up unused memory and get back resources.

Q3: What is garbage memory in data structure?


In data structures, garbage memory refers to memory occupied by data which is unnecessary or
unused.

Q4: Why is data structure used?


Data structures are used to organize and manage data efficiently, making it easier to store,
access, and manipulate information in programs

You might also like