Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 18, 2018 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/9c160db5aa9e9f3d370712cc0ac2b687 to your computer and use it in GitHub Desktop.
Save jianminchen/9c160db5aa9e9f3d370712cc0ac2b687 to your computer and use it in GitHub Desktop.
Leetcode 685: redudant connection - discussion with the peer on January 17, 2018 10:00 PM
/*
Leetcode
1 - 2 -3
| |
4 - 5
largest edge
The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N),
with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not
an edge that already existed.
The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents
an undirected edge connecting nodes u and v.
Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers,
return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
1
/ \
2 - 3
Example 2:
Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
| |
4 - 3
//////////////////////////
What are the algorithms for graphes? Peer asked himself.
dijkstra
belmanford
topological sort
Krusal algorithm (MST) <- Julia added
dfs / bfs
5
/ \
4 6
/ | \ / | \
1 2 3 -- 7 6 8
Julia shared the blogs to read:
https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
http://blog.csdn.net/dm_vincent/article/details/7655764
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment