Puzzles 45 to 47
Learn about graph traversal, lexicographical sorting, and chaining of set operations in Python.
We'll cover the following...
Puzzle 45
What is the output of the following code?
Note: Enter the output you guessed. Then, press the Run button, and your new Elo will be calculated. Don’t forget to save your Elo rating.
Graph traversal
What is a graph?
You already know data structures like lists, sets, and dictionaries. These data structures are denoted as complex data structures. But this is not because they’re difficult to understand but because they build upon other data structures.
A graph is just another complex data structure for relational data.
Relational data consists of edges and vertices. Each vertex stands in one or more relations with other vertices. An example of relational data is the Facebook social graph. Facebook represents users as vertices and friendship relations as edges. Two users are connected via an edge in the graph if they are (Facebook) friends.
How to maintain a graph data structure in the code
The puzzle uses an adjacency matrix as graph data structure G. Each row i in the matrix stores the out-neighbors of vertex i. And each column j stores the in-neighbors of vertex j. Thus, there is an edge from vertex i to vertex j: if G[i][j]==1.
How to determine whether there is a path between two vertices
Function ...