Strategies
1. Modify a known graph algorithms
- DFS and BFS don’t inherently provide output, so when we modify to do so then we’ve already done this strategy…
2. Use a known graph algorithm as a black box
- Provide the black box with some input.
- It’ll give us output. //Earth shattering!
// In reality we’ll probably do both simultaneously
Examples
Connected components
Problem: Given a graph G, how many connected components does G have?
- Strategy 1: Modify a known graph algorithm
- Solution idea: Use either DFS or BFS
- Add a counter to the “main loop”
- Count how many times (from main) the helper function is called
- Return the counter
Count map regions
- A board game is played on hex tiles.
- Tiles are drawn at random from a large supply.\
- Adjacent tiles of the same colour make up a region.
- Input data is a list of the colours for all tiles:
- colour(0,0) = red
- colour(4,4) = aqua
- Problem: determine how many regions are on the map. This map has 38 regions…

Solutions
Strategy 2 → Use the “connected components” algorithm as a black box
We need to construct a clever graph – just the right graph

It will encapsulate or model or represent our input problem in some way.
Finding the Right graph
- Must have vertices and edges, to represent the things and their relationships.
In this example it’s tiles as vertices and and their labels are grid points
Could connect based on color OR color and adjacency…but which to pick?

THE solution