Discover how graphs turn messy maps into clear, powerful tools for solving real-world problems!
Why Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Go?
Imagine you want to map out all the roads between cities on a map by writing down every connection manually on paper.
You try to remember which city connects to which, which roads go one way, and which have tolls or distances.
Writing and updating this map by hand is slow and confusing.
You might forget a road, mix up directions, or miss important details like distances or tolls.
It becomes hard to answer questions like "Can I get from city A to city B?" or "What is the shortest path?"
Graphs let us store all this information clearly in a structure made of points (vertices) and connections (edges).
We can mark edges as one-way (directed) or two-way (undirected), and add weights like distance or cost.
This makes it easy to find paths, check connections, and update the map quickly.
roads := map[string][]string{
"CityA": {"CityB", "CityC"},
"CityB": {"CityC"},
}
// No info on direction or weightstype Edge struct {
To string
Weight int
Directed bool
}
roads := map[string][]Edge{
"CityA": {{To: "CityB", Weight: 5, Directed: true}},
"CityB": {{To: "CityA", Weight: 5, Directed: true}},
}Graphs let us model complex networks clearly and efficiently, enabling powerful pathfinding and analysis.
GPS apps use graphs to represent roads (edges) and intersections (vertices), helping find the fastest route considering one-way streets and distances.
Graphs use vertices and edges to represent connections.
Edges can be directed (one-way) or undirected (two-way).
Weights add extra info like distance or cost to edges.
Practice
Solution
Step 1: Understand graph components
A graph consists of points called nodes and connections called edges.Step 2: Identify the term for nodes
The points or nodes where edges meet are called vertices.Final Answer:
Vertices -> Option DQuick Check:
Nodes = Vertices [OK]
- Confusing edges with vertices
- Thinking weights are nodes
- Mixing paths with vertices
Solution
Step 1: Understand adjacency list representation
An adjacency list uses a map from a vertex to a list of connected vertices.Step 2: Choose correct Go syntax
Using map[int][]int creates a map from int to slice of ints, perfect for adjacency list.Final Answer:
graph := make(map[int][]int) -> Option AQuick Check:
Map of int to slice[int] = adjacency list [OK]
- Using map[int]int which stores single int, not list
- Using slice of slices without map for keys
- Using slice of ints which can't map nodes
graph := map[int]map[int]int{
1: {2: 5, 3: 10},
2: {3: 2},
3: {},
}
fmt.Println(graph[1][3])Solution
Step 1: Understand graph structure
graph[1] is a map with keys 2 and 3, values 5 and 10 respectively.Step 2: Access graph[1][3]
graph[1][3] accesses the weight from vertex 1 to 3, which is 10.Final Answer:
10 -> Option CQuick Check:
Weight from 1 to 3 = 10 [OK]
- Confusing weight 5 (edge 1->2) with 1->3
- Assuming 0 means no edge but 3 has empty map
- Mixing up keys and values in nested maps
func addEdge(graph map[int][]int, u, v int) {
graph[u] = append(graph[u], v)
graph[v] = append(graph[u], v)
}Solution
Step 1: Analyze append statements
First append adds v to graph[u], correct for edge u->v.Step 2: Check second append
Second append incorrectly appends v to graph[u] again instead of graph[v].Final Answer:
Appending v to graph[v] incorrectly -> Option BQuick Check:
Second append should be graph[v] = append(graph[v], u) [OK]
- Appending to wrong slice variable
- Forgetting to append both directions
- Not initializing map slices before append
Solution
Step 1: Understand undirected graph edge representation
In undirected graphs, edges go both ways, so weights must be stored both ways.Step 2: Update both directions in map
Assign weight w to graph[u][v] and graph[v][u] to reflect undirected edge.Final Answer:
graph[u][v] = w; graph[v][u] = w -> Option AQuick Check:
Both directions updated = undirected edge [OK]
- Updating only one direction
- Assigning weight twice to same direction
- Confusing directed and undirected edge updates
Ready to prove it?
Pass the test and this pattern moves unknown
