Algorithm design, from static to dynamic.

Algorithm design is ignored by many, especially new programmers who tend to jump immediately into the code for any problem they are trying to solve without prior understand or proper analysis of the problem in hand. Regardless of its magnitude, jumping directly into “coding” is not always recommended. It is the superpower that they have recently unleashed “programming.”

This post analyzes a simple problem, tries to apply logic and rational steps to craft an appropriate solution. No programming background is required; no code will be demonstrated in this post. A simple workflow explaining how the process works following different scenarios. I assume you have prior knowledge of the basic flowchart components, including Start, Input, Condition, and Process. If not, please feel free to read about workflow basics here.

Simple traffic light scenario

A small town of 1000 people has only one traffic light. They analyzed their current needs and decided that the timings of the lights are as follows:

  • Green: 40 seconds.
  • Yellow: 2 seconds.
  • Red: 60 seconds.

How can we design a future proof traffic light?

Current situation analysis

The time dedicated to pedestrians crossing the streets is 50% higher than the time allowed for vehicles. The town population is 1000 people, but we do not know the number of cars they own. It is not clear how these figures (duration for each light) were set. So when the town residents grow to 2,000, 5,000, or 10,000 people, how the red/green/yellow light duration will be set?

If the relation is linear, then when the population grows to 2,000 people, the red-light duration will be 60*2 = 120 seconds? And for 10,000 people, it will be 60*10 = 600 seconds? Also, the time for yellow light 2*10 = 20 seconds. This will cause unrealistic delays for vehicles waiting at the traffic light as well as pedestrians.

Yellow is used to inform cars to slow down when the signal is changed from green to red, so this number can be set based on the maximum speed allowed, to give a fast car time to slow down ahead before the light is red.

Yellow also informs pedestrians to hurry to cross the street when it changes from red to green; this also can be configured based on the width of the street or number of lanes, to give pedestrians enough time to pass the full street width safely during such transition. The above specification is only for one traffic light, on one street, but this will be entirely different in the future when installing another one, or when having an intersection between two streets.

The following diagram shows the flowchart of the current traffic light with fixed timings. First, the red light starts for 60 seconds, so pedestrians can cross the street. Yellow light is on for 2 seconds, and finally, the green light goes on for 40 seconds for cars.

Simple workflow

Understanding the problem

The algorithm is always the same regardless of the street conditions. For example, the light will go red for 60 seconds stopping the cars even if no pedestrians are waiting to cross the street. The same happens when the light is green, preventing pedestrians from crossing the street while it might be empty with no cars passing. Depending on the case, the peak time for people and vehicles, the traffic light may be inefficient, causing many unwanted delays.

The solution, feedback, suggested scenario

A solution can be done by making the lights duration variably, dynamically set based on street status. This can be achieved by installing street sensors or cameras ahead of the traffic light and a simple push-button at the signal for pedestrians, those are additional inputs to the algorithm. So, if there are vehicles and no pedestrian, priority goes to the green light, if pedestrians are waiting and no cars, then priority goes to the red light. If both exist at the same time, it is up to the government to decide who can get priority.

The flowchart below demonstrates the newly updated solution

Improved workflow

Limitations, Future Improvements, and scenarios

The algorithm also doesn’t take action when cars or pedestrians break the rules. A camera monitoring the pedestrian passage can indicate how many times users are following the lights, a dedicated ANPR/LPR cameras are installed to record any violation for cars not stopping at the red light.

The scenario for traffic light should be tested against the objectives that led to installing this signal, not only against the algorithm’s simple steps. This test should always be updated when the software is updated to consider any new feature that also needs to be tested, and the same original objectives should be evaluated. The software might be working well.