Swift Algorithm Club: Graphs with Adjacency List
Learn how to implement directed and undirected graphs in Swift using adjacency lists. By Vincent Ngo.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Swift Algorithm Club: Graphs with Adjacency List
20 mins
- Getting Started
- Weighted Graph
- Directed and Undirected Graphs
- Representing a Graph
- Adjacency List
- Approach
- Implementing the Adjacency List
- Vertices
- Edges
- Graphable Protocol
- Adjacency List
- Creating Vertices
- Creating Edges
- Retrieving information
- Visualizing the adjacency list
- Testing out the Adjacency List
- Quiz Time
- Where to Go From Here?
Quiz Time
Use the graph created above to figure out how to use the adjacency list to solve the following problems:
How much does a ticket cost from Singapore to Toyko?
[spoiler title=”Solution”]
The answer is 500!
adjacencyList.weight(from: singapore, to: tokyo)
[/spoiler]
What are the flights going out of San Francisco?
[spoiler title=”Solution”]
if let flightsFromSanFrancisco = adjacencyList.edges(from: sanFrancisco) {
print("San Francisco Out Going Flights:")
print("--------------------------------")
for edge in flightsFromSanFrancisco {
print("from: \(edge.source) to: \(edge.destination)")
}
}
Flights going out are the following:
San Francisco Out Going Flights:
--------------------------------
from: San Francisco to: Hong Kong
from: San Francisco to: Washington DC
from: San Francisco to: Seattle
from: San Francisco to: Austin Texas
[/spoiler]
Where to Go From Here?
Here is the Swift Playground with all the code above. You can find alternative implementations and more information on graphs in the Swift Algorithm Club’s repository.
I hope you enjoyed this tutorial on making your first graph with an adjacency list! You have only scratched the surface of what graphs are capable of.
This was just one of the many algorithms from the Swift Algorithm Club’s repository. If you’re interested in more, check out the repo.
It’s in your best interest to know about algorithms and data structures. They’re solutions to many real world problems, and could come up during interviews. Plus it’s fun!
So stay tuned for many more tutorials from the Swift Algorithm club in the future. If you have any questions on graphs in Swift, please join the discussion below.
If you enjoyed what you learned in this tutorial, why not check out our Data Structures and Algorithms in Swift book, available on our store?
In Data Structures and Algorithms in Swift, you’ll learn how to implement the most popular and useful data structures and when and why you should use one particular datastructure or algorithm over another. This set of basic data structures and algorithms will serve as an excellent foundation for building more complex and special-purpose constructs.
As well, the high-level expressiveness of Swift makes it an ideal choice for learning these core concepts without sacrificing performance.
- You’ll start with the fundamental structures of linked lists, queues and stacks, and see how to implement them in a highly Swift-like way.
- Move on to working with various types of trees, including general purpose trees, binary trees, AVL trees, binary search trees and tries.
- Go beyond bubble and insertion sort with better-performing algorithms, including mergesort, radix sort, heap sort and quicksort.
- Learn how to construct directed, non-directed and weighted graphs to represent many real-world models, and traverse graphs and trees efficiently with breadth-first, depth-first, Dijkstra’s and Prim’s algorithms to solve problems such as finding the shortest path or lowest cost in a network.
- And much, much more!
By the end of this book, you’ll have hands-on experience solving common issues with data structures and algorithms — and you’ll be well on your way to developing your own efficient and useful implementations.