4.1.2.2 Graphs (Directed, Undirected, Weighted)
4.1.2.2 Graphs: Connecting Everything
Imagine a map of cities with roads connecting them, or a social media network where people are connected by friendships. A graph in computer science is a way to represent these kinds of connections! It's made up of:
- Nodes (or Vertices): These are the "dots" or "points" in the graph, representing things like cities, people, or web pages.
- Edges: These are the "lines" or "connections" between the nodes, representing roads, friendships, or links between web pages.
Graphs are super useful for showing how different things are related to each other.
Directed Graphs
In a directed graph, the connections (edges) have a specific direction, like one-way streets. An arrow usually shows the direction. If there's an arrow from Node A to Node B, it means you can go from A to B, but not necessarily from B to A.
- Example: A map of one-way streets, or links between web pages (you can click a link from page A to page B, but page B might not link back to page A).
Undirected Graphs
In an undirected graph, the connections (edges) don't have a specific direction. They're like two-way streets. If Node A is connected to Node B, it means you can go both ways between them.
- Example: A social media friendship: if you're friends with someone, they're also friends with you.
Weighted Graphs
In a weighted graph, each connection (edge) has a number attached to it, called a "weight." This weight can represent things like distance, time, or cost.
- Example: A map where the roads (edges) have numbers showing how long it takes to drive on them, or how far apart the cities are. This helps find the shortest or fastest path.
Bibliography for Graphs
- Masai School: Graph Data Structure - Explained With Examples
- Math Insight: Directed graph definition
- CodeChef: Graph Types - Undirected and Directed
- Medium (by Vaidehi Joshi): Finding The Shortest Path, With A Little Help From Dijkstra