0
0
DSA Cprogramming~15 mins

Graph Terminology Vertices Edges Directed Undirected Weighted in DSA C - Deep Dive

Choose your learning style9 modes available
Overview - Graph Terminology Vertices Edges Directed Undirected Weighted
What is it?
A graph is a way to show connections between things using points called vertices and lines called edges. Vertices represent objects, and edges show how these objects relate. Graphs can have directions on edges, meaning the connection goes one way, or no direction, meaning the connection is two-way. Sometimes edges have weights, which are numbers showing the strength or cost of the connection.
Why it matters
Graphs help us understand and solve problems involving relationships, like social networks, maps, or computer networks. Without graphs, it would be hard to organize or analyze these connections clearly. They let us find shortest paths, detect groups, or model real-world systems efficiently.
Where it fits
Before learning graphs, you should know basic data structures like arrays and lists. After graphs, you can learn graph algorithms like searching, shortest path, and spanning trees. Graphs build on simple connections and lead to complex problem solving.
Mental Model
Core Idea
A graph is a collection of points (vertices) connected by lines (edges) that can have direction and weight to represent relationships.
Think of it like...
Imagine a city map where intersections are vertices and roads are edges; some roads are one-way (directed), some two-way (undirected), and some roads have tolls or distances (weights).
Vertices: V1, V2, V3
Edges:
  V1 -- V2 (undirected)
  V2 -> V3 (directed)
  V1 --(5)--> V3 (weighted)

Diagram:

  V1
 /  \
V2 -> V3

Edges:
- V1 to V2: undirected (two-way)
- V2 to V3: directed (one-way)
- V1 to V3: weighted edge with weight 5
Build-Up - 7 Steps
1
FoundationUnderstanding Vertices as Points
🤔
Concept: Vertices are the basic units or points in a graph representing objects or entities.
Think of vertices as dots or points. Each vertex can represent anything like a person, a city, or a webpage. In code, vertices can be stored as numbers or labels. For example, vertices could be labeled 1, 2, 3 or named 'A', 'B', 'C'.
Result
You can identify and list all the points in a graph, like V = {1, 2, 3}.
Understanding vertices as simple points helps you see graphs as collections of objects before worrying about connections.
2
FoundationEdges Connect Vertices
🤔
Concept: Edges are lines that connect two vertices, showing a relationship or link between them.
Edges link pairs of vertices. For example, an edge between vertex 1 and vertex 2 means these two points are connected. Edges can be stored as pairs like (1, 2).
Result
You can represent connections like E = {(1, 2), (2, 3)}.
Seeing edges as connections between points builds the foundation for understanding graph structure.
3
IntermediateDirected vs Undirected Edges
🤔Before reading on: Do you think a directed edge means connection goes both ways or only one way? Commit to your answer.
Concept: Edges can have direction, meaning the connection flows one way (directed) or both ways (undirected).
In an undirected edge, the connection is two-way, like a friendship. In a directed edge, the connection goes from one vertex to another, like a one-way street. For example, (1 -> 2) means from vertex 1 to vertex 2 only.
Result
You can distinguish edges as undirected {(1, 2)} or directed {(1 -> 2)}.
Knowing edge direction changes how you interpret connections and affects graph algorithms.
4
IntermediateWeighted Edges Add Meaning
🤔Before reading on: Do you think weights on edges represent the number of connections or a cost/distance? Commit to your answer.
Concept: Edges can have weights, numbers that show cost, distance, or strength of the connection.
A weighted edge has a number attached, like (1 --5--> 2), meaning the connection from vertex 1 to 2 has weight 5. Weights help in finding shortest paths or cheapest routes.
Result
You can represent edges as triples like (1, 2, 5) where 5 is the weight.
Weights add depth to graphs, allowing modeling of real-world problems like travel cost or network delay.
5
IntermediateGraph Representation Methods
🤔
Concept: Graphs can be stored in different ways, mainly adjacency lists or adjacency matrices.
Adjacency list stores for each vertex a list of connected vertices. For example, vertex 1 connects to 2 and 3, so list is {1: [2,3]}. Adjacency matrix is a 2D grid where rows and columns are vertices, and cells show if an edge exists (and weight).
Result
You can store graph data efficiently for different operations.
Choosing the right representation affects performance and ease of graph operations.
6
AdvancedDirected Weighted Graphs in Practice
🤔Before reading on: Do you think directed weighted graphs can represent real-world networks like roads or social media? Commit to your answer.
Concept: Combining direction and weights models complex systems like traffic networks or influence graphs.
A directed weighted graph can show one-way roads with distances or social media followers with influence scores. For example, edge (A -> B, 10) means from A to B with cost 10. Algorithms use this to find shortest or cheapest paths.
Result
You can model and analyze real-world directional relationships with costs.
Understanding this combination is key to solving practical problems like GPS routing or recommendation systems.
7
ExpertEdge Cases and Graph Variants
🤔Before reading on: Can a graph have edges from a vertex to itself or multiple edges between same vertices? Commit to your answer.
Concept: Graphs can have loops (edges to self) and multiple edges (multigraphs), affecting algorithms and definitions.
A loop is an edge like (1 -> 1). Multigraphs allow multiple edges between same vertices, like two different roads between cities. These variants require special handling in algorithms and data structures.
Result
You recognize and handle special graph types beyond simple graphs.
Knowing these variants prevents bugs and helps choose correct algorithms for complex graphs.
Under the Hood
Internally, graphs are stored as collections of vertices and edges. Adjacency lists use arrays or linked lists to store neighbors for each vertex, saving space for sparse graphs. Adjacency matrices use 2D arrays where each cell indicates presence or weight of an edge, allowing fast edge lookup but using more memory. Directed edges are stored with order in pairs, and weights are stored as values associated with edges. Loops and multiple edges require additional data structures or representations.
Why designed this way?
Graphs were designed to model relationships naturally found in many fields like math, computer science, and social sciences. The choice of directed or undirected edges reflects real-world asymmetry or symmetry in connections. Weighted edges allow quantifying relationships. Different storage methods balance memory use and speed, chosen based on graph size and density.
Graph Storage Overview

+----------------+       +---------------------+
|  Adjacency     |       |  Adjacency Matrix   |
|  List          |       |                     |
|                |       |  V1 V2 V3 ...       |
| V1: [V2, V3]   |       | V1 0  1  5 ...      |
| V2: [V3]       |       | V2 0  0  1 ...      |
| V3: []         |       | V3 0  0  0 ...      |
+----------------+       +---------------------+

Directed edges stored as ordered pairs (from -> to).
Weights stored as values in lists or matrix cells.
Myth Busters - 4 Common Misconceptions
Quick: Do you think undirected edges have direction? Commit yes or no.
Common Belief:Undirected edges have direction because data structures store them as pairs.
Tap to reveal reality
Reality:Undirected edges do not have direction; they represent two-way connections without order.
Why it matters:Mistaking undirected edges as directed can cause wrong path calculations and algorithm errors.
Quick: Do you think weights on edges must be positive? Commit yes or no.
Common Belief:Edge weights are always positive numbers.
Tap to reveal reality
Reality:Weights can be zero, positive, or even negative depending on the problem, like in financial graphs.
Why it matters:Assuming only positive weights can lead to wrong algorithm choices and missed solutions.
Quick: Can a graph have multiple edges between the same two vertices? Commit yes or no.
Common Belief:Graphs never have more than one edge between the same two vertices.
Tap to reveal reality
Reality:Multigraphs allow multiple edges between the same vertices, representing different relationships or parallel connections.
Why it matters:Ignoring multiedges can cause loss of information and incorrect graph modeling.
Quick: Is a graph always connected? Commit yes or no.
Common Belief:All graphs are connected, meaning every vertex can reach every other vertex.
Tap to reveal reality
Reality:Graphs can be disconnected, having isolated vertices or groups with no paths between them.
Why it matters:Assuming connectivity can cause algorithms to fail or produce incomplete results.
Expert Zone
1
In directed graphs, edge direction affects traversal order and reachable vertices, which changes algorithm design significantly.
2
Weighted edges can represent different metrics simultaneously, requiring multi-dimensional weights or layered graphs.
3
Loops and multiedges complicate graph invariants and require careful handling in algorithms like cycle detection or shortest path.
When NOT to use
Graphs are not ideal when relationships are strictly hierarchical or linear; trees or lists are better. For very large dense graphs, adjacency matrices may waste memory; sparse representations or specialized data structures like compressed sparse row are preferred.
Production Patterns
Graphs model social networks with directed weighted edges for influence scores, road maps with weighted edges for distances, and dependency graphs in build systems. Professionals use adjacency lists for sparse graphs and matrices for dense graphs, and handle special cases like multigraphs in transport networks.
Connections
Network Theory
Graphs are the mathematical foundation of network theory, describing nodes and links.
Understanding graph terminology helps grasp complex network behaviors like flow, connectivity, and robustness.
Linear Algebra
Adjacency matrices of graphs are square matrices studied in linear algebra.
Knowing graph matrices allows applying matrix operations to analyze graph properties like connectivity and paths.
Urban Planning
Graphs model city layouts with intersections and roads, aiding traffic flow and infrastructure design.
Graph concepts translate directly to real-world city planning challenges, showing practical impact.
Common Pitfalls
#1Confusing directed edges as undirected, leading to wrong path directions.
Wrong approach:Edge list: {(1, 2), (2, 3)} treated as two-way connections without direction.
Correct approach:Edge list: {(1 -> 2), (2 -> 3)} explicitly showing direction.
Root cause:Misunderstanding that directed edges have order and flow only one way.
#2Ignoring edge weights when calculating shortest paths.
Wrong approach:Using BFS on weighted graph assuming all edges equal cost.
Correct approach:Using Dijkstra's algorithm that accounts for edge weights.
Root cause:Assuming unweighted algorithms work on weighted graphs.
#3Representing graph only with adjacency matrix for very large sparse graphs.
Wrong approach:Creating huge 2D matrix with mostly zeros wasting memory.
Correct approach:Using adjacency list to store only existing edges efficiently.
Root cause:Not considering graph density and memory efficiency.
Key Takeaways
Graphs consist of vertices (points) connected by edges (lines) that can be directed or undirected.
Directed edges have a one-way connection, while undirected edges connect both ways equally.
Weights on edges add meaning like cost or distance, enabling richer problem modeling.
Choosing the right graph representation (list or matrix) depends on graph size and density.
Understanding graph variants like loops and multiedges is essential for accurate modeling and algorithms.