Graph Theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.
Introduction To Graph Theory β¦FTC π π π
To store a graph, two methods are common:
- Adjacency Matrix
- Adjacency List
An adjacency matrix is a square matrix used to represent a finite graph. Adjacency Matrix (Storing Graphs) π π π
Adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbours of a vertex in a graph. It takes less memory to store graphs.
Let’s see a graph, and its adjacency matrix:
Now we create a list using these values.
This is called adjacency list. It shows which nodes are connected to which nodes. We can store this information using a 2D array. But will cost us the same memory as Adjacency Matrix.
Instead we are going to use dynamically allocated memory to store this one. Many languages support Vector or List which we can use to store adjacency list. For these, we don’t need to specify the size of the List. We only need to specify the maximum number of nodes.
The pseudo-code will be:
Procedure Adjacency-List(maxi, edgeN): //maxi denotes maximum number of nodes
edge[maxi] = Vector() // edgeN denotes number of edges
for i from 1 to edgeN
input -> x, y // Here x, y denotes there is an edge between x, y
edge[x].push(y)
edge[y].push(x)
end for
Return edge
Since this one is an undirected graph, it there is an edge from x to y, there is also an edge from y to x. If it was a directed graph, we’d omit the second one. For weighted graphs, we need to store the cost too. We’ll create another vector or list named cost[] to store these.
The pseudo-code:
Procedure Adjacency-List(maxi, EdgeN):
edge[maxi] = Vector()
cost[maxi] = Vector()
for i from 1 to EdgeN
input -> x, y, w
edge[x].push(y)
cost[x].push(w)
end for
Return edge, cost
From this one, we can easily find out the total number of nodes connected to any node, and what these nodes are.
It takes less time than Adjacency Matrix. But if we needed to find out if there’s an edge between u and v, it’d have been easier if we kept an adjacency matrix.
Introduction To Graph Theory β¦FTC π π π
Graph Theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.
Morae Q!
- Convert from binary number to decimal number.
- Swap two numbers by using division and multiplication.
- Compute foot and inches into centimetres.
- Concatenate Strings.
- Convert lowercase letters to uppercase letters.
- Compute the sum of all the digits of N.
- Compute the area of the pentagon.
- Get input using scanner.
- Find the distance between two points (x1,y1) and (x2,y2).
- Finding patterns in a file using frep and egrep .
- Detecting a cycle in a directed graph using Depth First Traversal.
- Topological ordering in a Graph.
- Storing graphs using adjacency list.
- Introduction to graph theory.
- Graph Adjacency Matrix pseudo code.
- Adjacency Matrix for storing graphs.
- Dijkstra’s shortest path algorithm .
- Breadth first search (BFS) Algorithm.
- Find the multiple of given number in the list of integers.
- Finding patterns in files using grep .