Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Notes On Graph

Loading...

1,030 Views

A graph is a set of connected nodes where each node is called a Vertex and the connection between two of them is called an Edge. We define a Graph as a data structure that consists of finite vertices and edges. With Cpp program.

Prantik S / Kolkata

11 years of teaching experience

Qualification: MCA (Jaipur National University - [JNU], Jaipur - 2017)

Teaches: Basic Computer, Computer for official job, MS Office, School Level Computer, ICT Training, Computer Science, Information Practice, IT & Computer Subjects, BCA Tuition, IT, Computer, C / C++, C# (C Sharp), Java And J2EE, Python Programming, Visual Basic, BCA Subjects, Hardware Training, Networking, Java Script

Contact this Tutor
  1. Graph Graphs are used to represent many real-life applications like networks, transportation paths of a city, and social networks such as Facebook where each person represents a single node. Graphs are useful in many applications of computer science as well. A graph is a set of connected nodes where each node is called a Vertex and the connection between two of them is called an Edge. We define a Graph as a data structure that consists of finite vertices and edges. There are two types of graphs, undirected (A) and directed (B): Undirected Graph Image A is an example of an undirected graph. Here, the edges do not have directions, meaning that if the vertex u is connected to v, then v is connected to u. Undirected graphs usually are drawn with straight lines between the vertices (no arrows). Example: On Facebook, if user A is a friend of user B, that automatically means that user B is a friend of user A.
  2. Directed Graph: Image B is an example of a directed graph. Here, each edge has a defined direction, which is usually represented with arrows. Having u connected to v does not necessarily mean that v is connected to u. Example: On Twitter, if user A follows user B, then user B does not necessarily follow user A back. Note: Two of the most commonly used implementations of graphs are: 1 . Adjacency Matrix 2. Adjacency List Check out the implementations below! An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not - we will use the value 1 for adjacent vertices, 0 - for not adjacent. 0 If the graph is undirected, the adjacency matrix is symmetric. A C++ code demonstration: #include using namespace std; class AdjacencyMatrix { private • int n; int **adj; public: AdjacencyMatrix(int n) { this->n = n;
  3. adj = new int*[n]; for (int k = 0; k < n; k++) { adj[k] = new int[n]; for(intj = 0; j < n; j++) { //Add a new edge to the Graph void addEdge(int orig, int dest) { if( orig > n Il dest > n Il orig < 1 Il dest < 1) { cout
  4. //Print the graph void display() { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) cout
  5. N/A