What is a Graph?

graph consists of a finite set of vertices or nodes and a set of edges connecting these vertices. Two vertices are said to be adjacent if they are connected to each other by the same edge.

Some basic definitions related to graphs are given below. You can refer to Figure 1 for examples.

  • **Order: **The number of vertices in the graph
  • **Size: **The number of edges in the graph
  • Vertex degree: The number of edges that are incident to a vertex
  • Isolated vertex: A vertex that is not connected to any other vertices in the graph
  • Self-loop: An edge from a vertex to itself
  • Directed graph: A graph where all the edges have a direction indicating what is the start vertex and what is the end vertex
  • Undirected graph: A graph with edges that have no direction
  • Weighted graph: Edges of the graph has weights
  • Unweighted graph: Edges of the graph has no weights

Image for post

Fig 1. Visualization of Terminology of Graphs (Image by Author)

1. Breadth-first search

Image for post

Fig 2. Animation of BFS traversal of a graph (Image by Author)

Traversing or searching is one of the fundamental operations which can be performed on graphs. In breadth-first search (BFS), we start at a particular vertex and explore all of its neighbours at the present depth before moving on to the vertices in the next level. Unlike trees, graphs can contain cycles (a path where the first and last vertices are the same). Hence, we have to keep track of the visited vertices. When implementing BFS, we use a queue data structure.

Figure 2 denotes the animation of a BFS traversal of an example graph. Note how vertices are discovered (yellow) and get visited (red).

Applications

  • Used to determine the shortest paths and minimum spanning trees.
  • Used by search engine crawlers to build indexes of web pages.
  • Used to search on social networks.
  • Used to find available neighbour nodes in peer-to-peer networks such as BitTorrent.

#computer-science #data-structures #data-science #algorithms #graph

10 Graph Algorithms Visually Explained
5.35 GEEK