Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1
Назад: Graphs
Дальше: Object-Oriented Graph Implementation

Directed Graphs

In some social networks, relationships are not mutual. For example, a social network may allow Alice to follow Bob, but Bob doesn’t have to follow Alice back. Let’s construct a new graph that demonstrates who follows whom:

/books/45079/OEBPS/graphs/graph_2.png

This is known as a directed graph. In this example, the arrows indicate the direction of the relationship. Alice follows both Bob and Cynthia, but no one follows Alice. We can also see that Bob and Cynthia follow each other.

We can still use our simple hash-table implementation to store this data:

 followees = {
 "Alice"​: [​"Bob"​, ​"Cynthia"​],
 "Bob"​: [​"Cynthia"​],
 "Cynthia"​: [​"Bob"​]
 }

The only difference here is that we’re using the arrays to represent the people each person follows.

Назад: Graphs
Дальше: Object-Oriented Graph Implementation