Unlocking Graphs: How Social Networks Map Relationships with Vertices and Edges
This article explains the concept of graphs as data structures, covering vertices, edges, directed and undirected graphs, degrees, and demonstrates how to store graphs using adjacency matrices and adjacency lists, comparing their space and time trade‑offs with clear examples and illustrations.
What Is a Graph?
In social networks like QQ, friendships can be represented as a graph , a data structure where each user is a vertex and each friendship is an edge . This models the complex web of connections between people.
Vertices, Edges, and Degree
Each vertex (node) may have multiple edges linking it to other vertices. The number of edges incident to a vertex is called its degree . In a directed graph, the number of outgoing edges is the out‑degree and the number of incoming edges is the in‑degree .
Directed vs. Undirected Graphs
A graph is undirected when edges have no orientation, resembling a mutual friendship. A directed graph adds arrows to edges, indicating a one‑way relationship such as “follows”.
Storing Graphs: Adjacency Matrix
The adjacency matrix (a two‑dimensional array) records connections between every pair of vertices. A cell contains 1 if an edge exists, otherwise 0. For example, an 8 × 8 matrix can represent eight people and their mutual relationships.
The matrix is simple but wastes space when the graph is sparse (many zeros), known as a sparse matrix.
Storing Graphs: Adjacency List
An adjacency list uses linked lists (or arrays of lists) where each vertex stores a list of its neighboring vertices, effectively recording only existing edges. This approach saves memory for sparse graphs.
Choosing Between the Two
Array (adjacency matrix) : fast edge lookup but high memory consumption; best for small or dense graphs.
Linked list (adjacency list) : memory‑efficient for large, sparse graphs but slower to check for a specific edge.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
