What is Graph?

<aside> 💡 Graph is a non-linear data structure that consist of node and edge. Sometimes Node also is called by knot and the edge is line or bow that connects 2 knots in a graph.

</aside>

Untitled

A Graph consist of finite sets of knot (or node) and sets of edges that connect a couple of nodes.

What is Vertex?

In graph theory, a vertex (plural: vertices) is a fundamental unit or a data point within a graph. It is often represented as a point or a node. Vertices are used to model entities or objects in a certain context, and they can be connected by edges to represent relationships or interactions between those entities.

For example, in a social network graph, each person can be represented as a vertex, and friendships between people are represented by edges connecting the corresponding vertices. In a geographical map, cities can be vertices, and roads or connections between cities can be edges.

The term "vertex" is synonymous with "node" in the context of graphs. Graphs consist of vertices and edges, where edges connect pairs of vertices. The relationships and connections between vertices in a graph are essential for analyzing and understanding various systems and structures.

Implementation

class adjnodes:
  def __init__(self, val):
      self.val = val
      self.next = None

class graph:
    def __init__(self, vertices):
        self.v = vertices
        self.ele = [None]*self.v

    def edge(self, src, dest):
        node = adjnodes(dest)
        node.next = self.ele[src]
        self.ele[src] = node

        node = adjnodes(src)
        node.next = self.ele[dest]
        self.ele[dest] = node

    def __repr__(self):
      for i in range(self.v):
        print("Adjacency list of vertex {}\\n head".format(i), end="")
        temp = self.ele[i]
        while temp:
              print(" -> {}".format(temp.val), end="")
              temp = temp.next

g = graph(4)
g.edge(0, 2)
g.edge(1, 3)
g.edge(3, 2)
g.edge(0, 3)
g.__repr__()
Adjacency list of vertex 0
 head -> 3 -> 2Adjacency list of vertex 1
 head -> 3Adjacency list of vertex 2
 head -> 3 -> 0Adjacency list of vertex 3
 head -> 0 -> 2 -> 1

The picture

   1
 / |
0--2
 \\ |
   3

Here, each vertex is represented by a circle, and edges are depicted by lines connecting the corresponding vertices. The adjacency list information is used to determine the connections between vertices. In this case, vertex 0 is connected to vertices 1 and 2, vertex 1 is connected to vertex 0, vertex 2 is connected to vertices 3 and 0, and vertex 3 is connected to vertices 0 and 1.