Data Structures · Graph Representation and Traversals
Applications of Graphs: Web Pages and Social Networks
Overview
Graphs, made of vertices and edges, model many real-world systems.
Two important examples are the World Wide Web (pages linked by hyperlinks) and social networks (people linked by friendship), and queries on these graphs, such as friend suggestions, are answered using traversals.
Key concepts
- Graph: a structure represented as a set of vertices and a set of edges.
- World Wide Web as a graph: every web page is a vertex; a link from one web page to another is an edge between the two vertices.
- Social network as a graph: every person with a profile is a vertex; if two people are friends, an edge exists between their vertices.
Taking yourself as a vertex, each of your friends is also a vertex, and an edge exists between your vertex and each friend's vertex (say A, B, ...). - Friend suggestions: the "this person might be your friend" suggestions on a social network such as Facebook are produced by performing traversals on the graph.
- Tree vs graph connectivity: a tree is connected, so starting from one particular node you can reach all other nodes; a graph may be disconnected, so starting from one node there is no guarantee of reaching every other node.

How it works
- Model the system as a graph: identify what each vertex represents and what each edge represents.
- Web: vertex = web page, edge = link from one page to another.
- Social network: vertex = person's profile, edge = friendship between two persons.
- Start from a vertex of interest (for example, your own vertex in the social network).
- Traverse the graph to explore the vertices reachable from it. The tree traversals already studied — preorder, inorder, postorder — are the starting point for understanding how such graph queries work.
- Use the traversal to answer the query; for example, friend suggestions are computed by performing traversals on the graph.
Comparison
| Property | Tree | Graph |
|---|---|---|
| Connectivity | Always connected — from any node all other nodes are reachable | May be disconnected — from a node, some nodes may be unreachable |
| Traversals seen | Preorder, inorder, postorder | Traversal may not always be possible from a single start node |
Revision summary
- Graph = set of vertices + set of edges.
- Web pages are vertices; hyperlinks between pages are edges.
- Social network profiles are vertices; friendship between two persons is an edge.
- Friend suggestions on social networks are computed using graph traversals.
- A tree is connected: every node is reachable from any node.
- A graph may be disconnected, so a traversal started at one node may not reach all nodes.
Previous-year exam insights
- A graph is directed when edges have a direction (e.g., hyperlinks, 'follows' relations) and undirected when edges are symmetric (e.g., mutual friendship); the web graph is directed, so reachability is not symmetric.
- Breadth-First Search (BFS) explores vertices level by level using a queue and finds shortest paths in terms of number of edges in an unweighted graph; Depth-First Search (DFS) explores as deep as possible using a stack/recursion.
- Both BFS and DFS take time with an adjacency list and with an adjacency matrix, and both need a visited array to avoid infinite loops on cycles.
- A connected component is a maximal set of vertices mutually reachable; counting components or checking connectivity requires restarting the traversal from each unvisited vertex.
- A tree on n vertices has exactly n - 1 edges and no cycles; a graph with n vertices and fewer than n - 1 edges is necessarily disconnected.
- In a directed graph, strongly connected components are maximal sets where every vertex reaches every other; this is found with Kosaraju's or Tarjan's algorithm, not with a single BFS/DFS from one vertex.