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 | 625 Views, 24694 Search Bots | 115 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 625 Views, 24694 Search Bots | 115 Words
| 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:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 625 Views, 24694 Search Bots | 115 Words 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']
Related Articles
- Daily Interview Problem: Reverse Words in a String
- Algorithm Interview Question: H-Index
- Algorithm Interview: Permutations of numbers
- Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
- Skip the readings, focus on problems. And use all the hints!
- Daily Interview Problem: Full Binary Tree
- Daily Interview Problem: Spiral Traversal of Grid
- Algorithm Interview: Level Order Traversal of Binary Tree
- Daily Interview Problem: Trapping Rainwater
- Daily Interview Puzzle: Falling Dominoes
©2006~2024 SteakOverCooked - 0.00933 Seconds(s) - 2006.373 KB/s - 33 Online Memory: 491.92 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !