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

In Perl: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair...
In Perl: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair to the dictionary. Change one of the values through reassignment. Access at least 3 values using the keys. Access at least 3 keys using the values. Delete an element. Test for key inclusion for 3 elements. Loop through the dictionary, printing out the key and value on the same line separated by a space. Key1, value1 Key2, value2 …,     … (Please if you...
In Python: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair...
In Python: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair to the dictionary. Change one of the values through reassignment. Access at least 3 values using the keys. Access at least 3 keys using the values. Delete an element. Test for key inclusion for 3 elements. Loop through the dictionary, printing out the key and value on the same line separated by a space. Key1, value1 Key2, value2 …,     … (Please if you...
Draw a directed graph consisting of five nodes, each of which is path connected to every...
Draw a directed graph consisting of five nodes, each of which is path connected to every other node in the graph. Be sure to label the vertices. I need LaTeX codes on this problem.
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...
Please use python: # Problem Description Given a directed graph G = (V, E), find the...
Please use python: # Problem Description Given a directed graph G = (V, E), find the number of connected components in G. # Input The graph has `n` vertices and `m` edges. There are m + 1 lines, the first line gives two numbers `n` and `m`, describing the number of vertices and edges. Each of the following lines contains two numbers `a` and `b` meaning there is an edge (a,b) belong to E. All the numbers in a line...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT