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

Related Articles

  1. [Daily Problem] Move Zeros
  2. How to Play WAV music under DOS?
  3. Daily Interview Problem: Distribute Bonuses
  4. Algorithm Interview Question: Nth Fibonacci Number
  5. Design Tic-Tac-Toe
  6. Detect Linked List Cycle
  7. CVE-2015-8874 - cPanel EasyApache Vulnerabilities
  8. Daily Interview Problem: Product of Array Except Self
  9. Algorithm Interview: Level Order Traversal of Binary Tree
  10. Floor and Ceiling of a Binary Search Tree

Comments (0)

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