Question

In: Computer Science

Use a dictionary to represent a directed graph in which each key is a pair (tuple)...

Use a dictionary to represent a directed graph in which each key is a pair (tuple) of two nodes, with the corresponding value set to the edge weight.

For example, W[u,v] =42.

Where in u → v.

Write a function to calculate the in and out degree of a given node.

What would be the advantages and disadvantages of this representation?


in python

Solutions

Expert Solution

CODE

graph = {}

def addEdge(u, v, weight):

graph[(u, v)] = weight

def getDegree(u):

indegree = 0

outdegree = 0

for key in graph:

(a, b) = key

if a == u:

outdegree += 1

if b == u:

indegree += 1

return (indegree, outdegree)

addEdge('a','c', 1)

addEdge('b','c', 3)

addEdge('b','e', 4)

addEdge('c','d', 1)

addEdge('c','e', 1)

addEdge('c','a', 2)

addEdge('c','b', 3)

addEdge('e','b', 2)

addEdge('d','c', 4)

addEdge('e','c', 6)

(indegree, outdegree) = getDegree('a')

print(indegree)

print(outdegree)


Related Solutions

Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
Let G = (V, E) be a directed acyclic graph modeling a communication network. Each link...
Let G = (V, E) be a directed acyclic graph modeling a communication network. Each link e in E is associated with two parameters, w(e) and d(e), where w(e) is a non-negative number representing the cost of sending a unit-sized packet through e, and d(e) is an integer between 1 and D representing the time (or delay) needed for transmitting a packet through e. Design an algorithm to find a route for sending a packet between a given pair of...
Process each line so you create dictionary where key is name of the company and value...
Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives as well. # Hint: Absolute value could be derived by...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
Use HSAB concepts to make predictions about which member of each pair should be more stable....
Use HSAB concepts to make predictions about which member of each pair should be more stable. Explain your reasoning in each case. (a) (PtCl4)2- or (PtF4)2- (b) [Fe(H2O)6]3+ or [Fe(PH3)6]3+ (c) F3B:THF or Cl3B:THF (d) (CH3)3B:PCl3 or (CH3)3B:P(CH3)3 (e) (CH3)3Al:pyridine or (CH3)3Ga:pyridine
Which molecule or formula unit in each pair has the greater dipole moment? Use polar arrows...
Which molecule or formula unit in each pair has the greater dipole moment? Use polar arrows to indicate the bond polarity of each bond in the species and then indicate overall polarity: a)    HCl or HI b)   BF3 or NF3 c)    AlCl3 or CHCl3
For the following exercises, use the table of values that represent points on the graph of a quadratic function. By determining..
For the following exercises, use the table of values that represent points on the graph of a quadratic function. By determining the vertex and axis of symmetry, find the general form of the equation of the quadratic function.
Which pair of quantities represent scalar quantities? (1) displacement and velocity (2) displacement and time (3)...
Which pair of quantities represent scalar quantities? (1) displacement and velocity (2) displacement and time (3) energy and velocity (4) energy and time
Consider the classical IS-LM/AS-AD model under the assumptions of the misperceptions theory. A) Which graph represent...
Consider the classical IS-LM/AS-AD model under the assumptions of the misperceptions theory. A) Which graph represent the classical AS-AD model under the assumptions of the misperceptions theory? Which curve represents the SRAS curve? What about the LRAS and AD curves? B) Assume the economy is starting in general equilibrium. Using the classical AS-AD model with misperceptions, describe what will happen in the short-run and the long-run to output, the price level, and the expected price level, and when there is...
Question 13. For each pair of variables, specify which variable is the explanatory variable and which...
Question 13. For each pair of variables, specify which variable is the explanatory variable and which is the response variable. Also identify whether the variable is categorical or numerical. The time student spends for the study and her/his final score The car engine size and car’s maximum speed
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT