0% found this document useful (0 votes)
28 views

DSA Day 4

Uploaded by

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

DSA Day 4

Uploaded by

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

UET LAHORE

NEW CAMPUS

Programming
Session
Phase 1
DSA
Graph
A graph is a non-linear data structure. It is a collection of nodes connected to each other by edges. Each node
contains a data field.
A graph is a set of pairs – (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs of
vertices.

V = {1, 2, 3, 4, 5, 6, 7, 8, 9}
E = {1-2, 1-3, 1-9, 1-6, 2-7, 2-8, 7-9, 8-3, 9-6, 3-6}

Properties
Adjacency − Two nodes or vertices are adjacent if they are connected through an edge. In the following example, B
is adjacent to A, C is adjacent to B, and so on.
Path − Path represents a sequence of edges between the two vertices. In the following example, ABCD represents a
path from A to D.
Self-Loop − Is an edge that connects a vertex to itself. A simple graph contains no loops.
Multi Edge − two or more edges that are connecting to the same two vertices.
Simple Graph − Graphs without loops or parallel edges are called simple graphs.
The degree of a node − The degree of a node is the no of edges incident/attached on it.
Path − A path can be defined as the sequence of nodes that are followed in order to reach some terminal node E
from the initial node A.
Simple Path − A path is a Simple path if no vertices(and thus edges) are not repeated
Cycle − A cycle can be defined as a path which has no repeated edges or vertices except the first and last vertices.

Types of graph
A weight is a numerical value attached to each edge in the graph.
A weighted Graph will contain weight on each edge whereas an unweighted does not.
Directed Graphs – A directed graph is a set of vertices (nodes) connected by edges, with each node having a direction
associated with it. Edges are usually represented by arrows pointing in the direction the graph can be traversed.
Undirected Graphs – In an undirected graph the edges are bidirectional, with no direction associated with them

More Types of Graph

Application
Social networks - This is one of the most basic applications of a graph. A social graph illustrates connections
among people and organizations in a social network. Example: Facebook

Biological networks - Remember when we told you that the whole universe can be a graph? We were not lying.
Our environment is also a huge graph that includes multiple hierarchies, brain networks, food chains, etc.
Road networks/navigation - In the computer science world, all navigation problems are fittingly termed graph
problems. In a huge city with hundreds of roads and alleys, graphs guide you from point A to point B.

You would have seen how the roads are connected in the form of vertices and edges on many modern apps like
Google Maps, Uber, Maze, etc.

Blockchains - One thing you must have heard of in recent years is blockchain technology. It is being deemed to
be the future of all transactions.

Implementation
When it comes to implementation of Graph DS in Computer Applications the following two are the most
commonly used representations of a graph –
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of booleans (0's and 1's). A finite graph can be
represented in the form of a square matrix on a computer, where the boolean value of the matrix indicates if
there is a direct path between two vertices
Adjacency List
An adjacency list represents a graph as an array of linked lists. The index of the array represents a vertex and
each element in its linked list represents the other vertices that form an edge with the vertex.

The VxV space requirement of the adjacency matrix makes it a memory hog. Graphs out in the wild usually
don't have too many connections and this is the major reason why adjacency lists are the better choice for most
tasks.
However, Finding the adjacent list is not quicker than the adjacency matrix because all the connected nodes
must be first explored to find them.

Matrix
#include <iostream>
using namespace std;

class Graph {
private:
bool** adjMatrix;
int numVertices;

public:
// Initialize the matrix to zero
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new bool* [numVertices];
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = false;
}
}

// Add edges
void addEdge(int i, int j) {
adjMatrix[i][j] = true;
adjMatrix[j][i] = true;
}

// Remove edges
void removeEdge(int i, int j) {
adjMatrix[i][j] = false;
adjMatrix[j][i] = false;
}

// Print the martix


void toString() {
for (int i = 0; i < numVertices; i++) {
cout << i << " : ";
for (int j = 0; j < numVertices; j++)
cout << adjMatrix[i][j] << " ";
cout << "\n";
}
}

~Graph() {
for (int i = 0; i < numVertices; i++)
delete[] adjMatrix[i];
delete[] adjMatrix;
}
};

List
#include<iostream>
#include<vector>
using namespace std;

// Add edge
void addEdge(vector<int> adj[], int s, int d) {
adj[s].push_back(d);
adj[d].push_back(s);
}

// Print the graph


void printGraph(vector<int> adj[], int V) {
for (int d = 0; d < V; ++d) {
cout << "\n Vertex "
<< d << ":";
for (auto x : adj[d])
cout << "-> " << x;
printf("\n");
}
}

int main() {
const int V = 5;

// Create a graph
vector<int> adj[V];

// Add edges
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 0, 3);
addEdge(adj, 1, 2);
printGraph(adj, V);
}

Spanning tree
A spanning tree is a sub-graph of an undirected connected graph, which includes all the vertices of the graph
with a minimum possible number of edges. If a vertex is missed, then it is not a spanning tree.
Application
Minimum spanning trees have direct applications in the design of networks, including computer networks,
telecommunications networks, transportation networks, water supply networks, and electrical grids

Kruskal's Algorithm
Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of the
edges of that graph which form a tree that includes every vertex has the minimum sum of weights among all the
trees that can be formed from the graph.

• Sort all the edges from low weight to high.


• Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle,
then reject this edge.
• Keep adding edges until we reach all vertices.
Watch

Prim’s Algorithm
Prim's algorithm is a minimum-spanning tree algorithm
The steps for implementing Prim's algorithm are as follows:
• Initialize the minimum spanning tree with a vertex chosen at random.
• Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree
• Keep repeating step 2 until we get a minimum spanning tree

Watch 1
Watch 2

To be continued……………………………………………

You might also like