To the Top
File:  root - text - article - 2020 - 02 - reverse-a-directed-graph.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 255 Views, 19901 Search Bots | 115 Words

Subscribe to Feed Burner | Browse | Archive
Hi, here's your problem today. This problem was recently asked by Facebook:

Given a directed graph, reverse the directed graph so all directed edges are reversed.

Example:
Input:
A -> B, B -> C, A ->C

Output:
B->A, C -> B, C -> A
Here's a starting point:


from collections import defaultdict

class Node:
def __init__(self, value):
self.adjacent = []
self.value = value

def reverse_graph(graph):
# Fill this in.

a = Node('a')
b = Node('b')
c = Node('c')

a.adjacent += [b, c]
b.adjacent += [c]

graph = {
a.value: a,
b.value: b,
c.value: c,
}

for _, val in reverse_graph(graph).items():
print(val.adjacent)
# []
# ['a', 'b']
# ['a']
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 255 Views, 19901 Search Bots | 115 Words Subscribe to Feed Burner

Related Articles

  1. New Vulnerabilities (CVE-2016-4581) have been detected in CentOS/RHEL/CloudLinux 7
  2. Windows Scripting
  3. Number of Ways to Climb Stairs
  4. Most Frequent Subtree Sum
  5. Daily Interview Question: Edit Distance
  6. Making a Height Balanced Binary Search Tree
  7. A trick for getting good at coding interviews FASTER
  8. Daily Interview Question: Find Cycles in a Graph
  9. Delphi Dynamic Array
  10. [Daily Problem] Add two numbers as a linked list

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />