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

Data Structures and Algorithms - #12 (1)

The document outlines the implementation of a graph structure using adjacency list representation, defining key terminology such as vertices, edges, adjacent nodes, and various types of paths and graphs. It also describes basic operations for graph manipulation, including adding vertices and edges, and provides an example of an undirected graph. Additionally, it includes C++ code snippets for constructing a graph and performing matrix transposition using graph properties.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structures and Algorithms - #12 (1)

The document outlines the implementation of a graph structure using adjacency list representation, defining key terminology such as vertices, edges, adjacent nodes, and various types of paths and graphs. It also describes basic operations for graph manipulation, including adding vertices and edges, and provides an example of an undirected graph. Additionally, it includes C++ code snippets for constructing a graph and performing matrix transposition using graph properties.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DATA STRUCTURES Lab #12

Implementation of Graph
Structure Through Adjacency
AND ALGORITHMS List Representation
.
.
A, B, C, and D are the vertices of the graph.
2.Edge: The link or path between two vertices is called an edge. It
Graph
connects twoTerminology
or more vertices. The different edges in the above graph
are AB, BC, AD, and DC.
3.Adjacent node: In a graph, if two nodes are connected by an edge
then they are called adjacent nodes or neighbors. In the above graph,
vertices A and B are connected by edge AB. Thus A and B are adjacent
nodes.
4.Degree of the node: The number of edges that are connected to a
particular node is called the degree of the node. In the above graph,
node A has a degree 2.
5.Path: The sequence of nodes that we need to follow when we have
to travel from one vertex to another in a graph is called the path. In our
example graph, if we need to go from node A to C, then the path would
be A->B->C.
.
6.Closed path: If the initial node is the same as a terminal node, then
that path is termed as the closed path.
7.Simple path: A closed path in which all the other nodes are distinct
is called a simple path.
8.Cycle: A path in which there are no repeated edges or vertices and
the first and last vertices are the same is called a cycle. In the above
graph, A->B->C->D->A is a cycle.
9.Connected Graph: A connected graph is the one in which there is a
path between each of the vertices. This means that there is not a single
vertex which is isolated or without a connecting edge. The graph shown
above is a connected graph.
10.Complete Graph: A graph in which each node is connected to
another is called the Complete graph. If N is the total number of nodes
in a graph then the complete graph contains N(N-1)/2 number of edges.
11.Weighted graph: A positive value assigned to each edge
indicating its length (distance between the vertices connected by an
edge) is called weight. The graph containing weighted edges is called a
weighted graph. The weight of an edge e is denoted by w(e) and it
indicates the cost of traversing an edge.
12.Diagraph: A digraph is a graph in which every edge is associated
.
.

Basic Operations For Graphs

Following are the basic operations that we


can perform on the graph data structure:
•Add a vertex: Adds vertex to the graph.
•Add an edge: Adds an edge between the two
vertices of a graph.
•Display the graph vertices: Display the vertices
of a graph.
Undirected Graph Example:
Let’s assume we have the following graph with 5
vertices (0, 1, 2, 3, 4) and the following edges:
•0 - 1
•0 - 4
•1 - 2
•1 - 3
•1 - 4
•3 - 4
This graph is undirected, meaning if there’s an
edge between vertex 0 and vertex 1, it implies
there’s also an edge between vertex 1 and vertex
0.
#include <iostream>
// Constructor to initialize the graph with 'V'
#include <list> // For using the list
vertices
container
Graph::Graph(int V) {
#include <iterator>
this->V = V;
using namespace std;
adj = new list<int>[V]; // Create an array of
lists
// Class to represent a Graph using
}
Adjacency List
class Graph {
// Function to add an undirected edge to the
int V; // Number of vertices
. list<int>* adj; // Pointer to an array graph
void Graph::addEdge(int v, int w) {
containing adjacency lists
adj[v].push_back(w); // Add w to v's list
adj[w].push_back(v); // Add v to w's list
public:
(undirected edge)
// Constructor
}
Graph(int V);
// Function to print the adjacency list
// Add edge to the graph (undirected)
representation of the graph
void addEdge(int v, int w);
void Graph::printGraph() {
for (int v = 0; v < V; v++) {
// Print the adjacency list of the graph
cout << "Vertex " << v << ": ";
void printGraph();
for (auto x : adj[v]) {
};
cout << x << " ";
}
Write a program that performs the Transpose
operation in Matrix by using the graph properties
#include <iostream>
int main() {
using namespace std;
// Original matrix (Adjacency Matrix
representation of a graph)
// Function to perform transpose of a matrix using graph
int matrix[3][3] = {
properties
{0, 1, 0},
void transposeMatrix(int matrix[][3], int transpose[][3], int
{1, 0, 1},
rows, int cols) {
{0, 1, 0}
// Swap elements to transpose the matrix
};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int transpose[3][3]; // Matrix to store the
transpose[j][i] = matrix[i][j]; // Swap row and
. transpose
column
int rows = 3, cols = 3;
}
}
// Display the original matrix
}
cout << "Original Matrix (Adjacency Matrix):"
<< endl;
// Function to display the matrix
displayMatrix(matrix, rows, cols);
void displayMatrix(int matrix[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
// Perform the transpose operation
for (int j = 0; j < cols; j++) {
transposeMatrix(matrix, transpose, rows, cols);
cout << matrix[i][j] << " ";
}
// Display the transposed matrix
cout << endl;
cout << "\nTransposed Matrix:" << endl;
}
displayMatrix(transpose, rows, cols);
}
return 0;
.

You might also like