Graph guide #1
temptation4
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
A graph is a non-linear data structure used to represent relationships between different objects.

Each object is called a vertex (node)
The connection between two nodes is called an edge
👉 In simple words:
Graph = Nodes + Connections
Circles → Vertices (Nodes)
Lines → Edges (Connections)
🔁 Graph Traversal
🌳 Breadth-First Search (BFS)
👉 Explores nodes level by level
👉 Uses Queue (FIFO)
👉 Best for shortest path (unweighted graph)
🌳 Depth-First Search (DFS)
👉 Goes deep first, then backtracks
👉 Uses Stack / Recursion
👉 Useful for cycle detection, path finding
🧱 Graph Building (Step-by-Step)
Below image shows how a graph is built step-by-step using edges.

🔹 Step 1: Start with single node
Only node 1 exists
No connections
Add node 2 → connect to 1
Add node 3 → connect to 1
Add node 4 → connect to 3
Add node 5 → connect to 2
Add node 6 → connect to 1
💻 Java Code for creating Graph
👤 Example
A = Person
B = Person
Edge = Friendship
🔗 Real-Life Examples of Graphs
👉 Real-world graphs represent:
📌 Types of Graphs
1. Directed vs Undirected Graph
🔹 Undirected Graph
Edges have no direction → A — B
🔹 Directed Graph
Edges have direction → A → B
2. Weighted vs Unweighted Graph
🔹 Weighted → edges have cost (distance/time)
🔹 Unweighted → all edges equal
3. Cyclic vs Acyclic Graph
🔹 Cyclic → contains loop
🔹 Acyclic → no loop
📊 Graph Representation (VERY IMPORTANT)
1. Adjacency Matrix
👉 2D array representation
👉
1→ edge exists👉
0→ no edge2. Adjacency List (Most Used)
👉 Stores neighbors of each node
👉 More memory efficient than matrix
⚡ Quick Summary
🎯 Why Graphs Matter
Graphs are heavily used in:
👨💻 Author
Neelu Sahai
Beta Was this translation helpful? Give feedback.
All reactions