Data Structure 27+28
Data Structure 27+28
Lecture : 27 + 28
Instructor: Tayyba Khalid
Content:
■ 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.