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, 24715 Search Bots | 115 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 625 Views, 24715 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, 24715 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
- New Vulnerabilities (CVE-2016-4581) have been detected in CentOS/RHEL/CloudLinux 7
- Algorithm Interview: Convert Roman Numerals to Decimal
- Daily Interview Problem: Validate Binary Search Tree
- Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
- CVE-2015-8874 - cPanel EasyApache Vulnerabilities
- Algorithm Interview Question: Max and Min with Limited Comparisons
- Daily Interview Puzzle: Falling Dominoes
- Consecutive Ones
- Most Frequent Subtree Sum
- Algorithm Interview: No Adjacent Repeating Characters
©2006~2024 SteakOverCooked - 0.00684 Seconds(s) - 2941.816 KB/s - 42 Online Memory: 494.3 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="index, 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 !