Dynamic Programming--minium span Tree

Source: Internet
Author: User

graph=[
[0, 1, 999, 9, 999],
[1, 0, 5, 7, 3],
[999, 5, 0, 999, 4],
[9, 7, 999, 0, 3],
[999, 1, 999, 9, 999]
]

For I in Range (0,5):
Print Graph[i]

Points =[' A ', ' B ', ' C ', ' D ', ' E ']
Selected =[]

def is_not_selected (i):
If I in selected:
Return False
Else
Return True

edges = []
#1 Find the shortest edge
#2 Add the points of edge into selected set
#3 in the selected set, add the shortest edge with one of the selected set,while the
def get_minium_tree (edges):
K=0
While k<100:
min=1000
Curi =-1
Curj =-1
For I in Range (0,5):
For j in Range (0,5):
if ((I!=j) and (is_not_selected (i) or is_not_selected (j)):
If MIN&GT;GRAPH[I][J]:
Min= Graph[i][j]
Curi =i
Curj = J
if ((Curi = =-1) or (Curj = =-1)):
Break
If Is_not_selected (Curi):
Selected.append (Curi)
If Is_not_selected (CURJ):
Selected.append (CURJ)
Edges.append ([Points[curi], Points[curj]])
K=k+1

Get_minium_tree (edges)

Print edges

Dynamic Programming--minium span Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.