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

Related Articles

  1. Daily Interview Problem: Contiguous Subarray with Maximum Sum
  2. Compare Version Numbers
  3. Daily Interview Problem: Maximum Profit From Stocks
  4. Daily Interview Problem: Longest Substring With K Distinct Characters
  5. Algorithm Interview: Maximum Path Sum in Binary Tree
  6. Daily Interview Problem: Buddy Strings
  7. Reverse a Directed Graph
  8. Find Pythagorean Triplets
  9. Skip the readings, focus on problems. And use all the hints!
  10. Find the non-duplicate number

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: May 11 2024 14:36:49 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />