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, 19906 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, 19906 Search Bots | 115 Words Subscribe to Feed Burner

Related Articles

  1. Algorithm Interview: Maximum Path Sum in Binary Tree
  2. Daily Interview Problem: Reverse Words in a String
  3. Daily Interview Question: Edit Distance
  4. YES!!
  5. Daily Interview Question: Longest Sequence with Two Unique Numbers
  6. Daily Interview Problem: Minimum Removals for Valid Parenthesis
  7. Compare Version Numbers
  8. Most Frequent Subtree Sum
  9. [Daily Problem] Longest Palindromic Substring
  10. Daily Interview Problem: Jump to the End

Comments (0)

    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" />