6 kyu

Make a spanning tree

70 of 119FArekkusu

Description:

Idea

In the world of graphs exists a structure called "spanning tree". It is unique because it's created not on its own, but based on other graphs. To make a spanning tree out of a given graph you should remove all the edges which create cycles, for example:

This can become      this      or      this        or       this

 A                    A                  A                   A
 |\                   |                   \                  |\
 | \       ==>        |                    \                 | \
 |__\                 |__                 __\                |  \
B   C                 B  C               B  C                B  C

Each edge (line between 2 vertices, i.e. points) has a weight, based on which you can build minimum and maximum spanning trees (sum of weights of vertices in the resulting tree is minimal/maximal possible).
Wikipedia article on spanning trees, in case you need it.


Task

You will receive an array like this: [["AB", 2], ["BC", 4], ["AC", 1]] which includes all edges of an arbitrary graph and a string "min"/"max". Based on them you should get and return a new array which includes only those edges which form a minimum/maximum spanning trees.

edges = [("AB", 2), ("BC", 4), ("AC", 1)]

make_spanning_tree(edges, "min")    ==>    [("AB", 2), ("AC", 1)]
make_spanning_tree(edges, "max")    ==>    [("AB", 2), ("BC", 4)]
edges = [["AB", 2], ["BC", 4], ["AC", 1]]

makeSpanningTree(edges, "min")    ==>    [["AB", 2], ["AC", 1]]
makeSpanningTree(edges, "max")    ==>    [["AB", 2], ["BC", 4]]
input = [["AB", 2], ["BC", 4], ["AC", 1]]

make_spanning_tree(edges, "min")    ==>    [["AB", 2], ["AC", 1]]
make_spanning_tree(edges, "max")    ==>    [["AB", 2], ["BC", 4]]

Notes

  • All vertices will be connected with each other
  • You may receive cycles, for example - ["AA", n]
  • The subject of the test are these 3 values: number of vertices included, total weight, number of edges, but you should not return them, there's a special function which will analyze your output instead
Algorithms
Trees
Graph Theory

Stats:

CreatedMay 1, 2018
PublishedMay 1, 2018
Warriors Trained602
Total Skips21
Total Code Submissions1429
Total Times Completed119
Python Completions70
JavaScript Completions53
Ruby Completions6
Total Stars19
% of votes with a positive feedback rating96% of 35
Total "Very Satisfied" Votes32
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • FArekkusu Avatar
  • user6793616 Avatar
Ad