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 | 659 Views, 25808 Search Bots | 115 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 659 Views, 25808 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 | 659 Views, 25808 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
- Algorithm Interview: Permutations of numbers
- Daily Interview Problem: Trapping Rainwater
- Algorithm Interview Question: Symmetric k-ary Tree
- Reverse a Directed Graph
- Progess made
- Daily Interview Problem: Tree Serialization
- Daily Interview Problem:Create a balanced binary search tree
- YES!!
- CVE-2015-8874 - cPanel EasyApache Vulnerabilities
- Spectrum Master
©2006~2024 SteakOverCooked - 0.00751 Seconds(s) - 2588.086 KB/s - 9 Online Memory: 493.31 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 !