In this part you will build the actual graph that keeps track of passes between players and performs simple analyses.
We use an adjacency list representation: for each sender (player) we store a list of outgoing Pass
objects.
A graph consists of nodes and edges.
In our application, a node is a player and an edge is a pass between two players.
We use an adjacency list representation:
nr_of_times
, the number of times this pass occurred in the matchImplement a class PassGraph
that allows you to:
From Part 1 and Part 2 you already have:
Player(name: str, number: int)
Pass(sender: Player, receiver: Player, nr_of_times: int)
with:
get_weight()
→ nr_of_times
get_start()
→ sender
get_end()
→ receiver
__eq__
compares only sender
and receiver
__str__
returns "Pass from <sender> to <receiver>"
PassGraph
players
— list with all players in the graph.adj
— dictionary with sender name as key and a list of Pass
objects as value (all passes from that sender).add_player
with player
as argument
player
if no other player with the same name
exists.adj
.has_player
with player
(object or name) as argument
True
if the player (or name) is in the graph.Player
object and a str
(name).get_player
with name
as argument
Player
with this name, or None
if it does not exist.add_pass
with arguments sender
(Player object), receiver
(Player object), and times
(int, default = 1).
sender → receiver
already exists, increment nr_of_times
by times
.Pass(sender, receiver, times)
and add it to adj
at the correct sender.times <= 0
, either ignore or raise ValueError
.get_pass
with arguments sender_name
(str) and receiver_name
(str)
None
.neighbors
with argument sender_name
(str)
total_weight
with argument subset
(a list of player names)
nr_of_times
for all passes where both sender and receiver are in the subset.subset
is None
: take all players.subset
is always a list of player names.Method pass_intensity(self, subset: list[str] | None = None) -> float
with argument subset
(list of player names)
This method computes how strongly a group of players pass among each other.
The intensity is defined as:
\(\text{pass_intensity} = \frac{\text{total number of passes within the subset}}{\text{maximum possible number of directed passes within the subset}}\)
nr_of_times
for passes where both sender and receiver are in the subset.Special cases
0.0
.Example
top_pairs
with argument k
(int, default = 5)
nr_of_times
(globally).distribution_from
with argument sender_name
(str)
(receiver_name, count)
sorted descending by count.None
or []
(as specified), but do not raise exceptions in normal query methods.PassGraph
.Create the following test scenario:
PassGraph
objectPassGraph