import 'dart:collection'; void main(List args) { final graph = >{}; graph.addAll( >{ 'you': ['alice', 'bob', 'claire'], 'bob': ['anuj', 'peggy'], 'alice': ['peggy'], 'claire': ['thom', 'jonny'], 'anuj': [], 'peggy': [], 'thom': [], 'jonny': [], }, ); search(graph, 'you'); } bool search(Map> graph, String name) { final searchQueue = Queue()..addAll(graph[name] ?? []); final searched = List.empty(growable: true); while (searchQueue.isNotEmpty) { final String person = searchQueue.removeFirst(); if (searched.contains(person) == false) { if (_personIsSeller(person)) { print('$person is a Mango seller!'); return true; } else { searchQueue.addAll(graph[person] ?? []); searched.add(person); } } } return false; } bool _personIsSeller(String name) { return name.endsWith('m'); }