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

Related Articles

  1. Daily Interview Problem: Contiguous Subarray with Maximum Sum
  2. Daily Interview Problem: Running Median
  3. Binary Tree Level with Minimum Sum
  4. Plus One
  5. Daily Interview Problem: Deepest Node in a Binary Tree
  6. Daily Interview Problem:Create a balanced binary search tree
  7. Reverse a Linked List
  8. Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
  9. Daily Interview Problem: Validate Binary Search Tree
  10. Algorithm Interview: Longest Consecutive Sequence

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="index, follow">