第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

流程圖獲取深度,求各位算法高手幫幫忙

流程圖獲取深度,求各位算法高手幫幫忙

慕森王 2018-11-21 17:14:11
最近這個(gè)問(wèn)題困擾我半天,我有以下json數(shù)據(jù)其中,prev_node代表上一個(gè)節(jié)點(diǎn),next_node為下一節(jié)點(diǎn).如果prev_node為Null,則代表當(dāng)前為第一個(gè)節(jié)點(diǎn).next_node為null為最后一個(gè)節(jié)點(diǎn).根據(jù)數(shù)據(jù)得到以下流程圖求當(dāng)前深度最深的流程有哪些節(jié)點(diǎn),和流程的有幾個(gè)分支注:節(jié)點(diǎn)只能向下走
查看完整描述

1 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

#!/usr/bin/python

# coding: utf-8



def build_graph(nodes=[]):

    graph = {}

    for node in nodes:

        if not node['prev_node']:      # root node

            if 'root' not in graph:

                graph['root'] = []

            graph['root'].append(node['next_node'])

        else:

            if node['prev_node'] not in graph:

                graph[node['prev_node']] = []

            if node['next_node']:

                graph[node['prev_node']].append(node['next_node'])

    return graph



def travel(node, graph={}):

    # print(node)

    if not graph[node]:

        return 1, [node]   # branch, node


    max_nodes = []

    max_branches = 0

    for i in graph[node]:

        branches, nodes = travel(i, graph)

        if len(nodes) > len(max_nodes):

            max_nodes = nodes

        max_branches += branches

    max_nodes.append(node)

    return max_branches, max_nodes


if __name__ == '__main__':


    nodes = [

        {"prev_node": "0000000000000005","next_node": "0000000000000006"},

        {"prev_node": "0000000000000006","next_node": "0000000000000007"},

        {"prev_node": "0000000000000006","next_node": "0000000000000008"},

        {"prev_node": "0000000000000008","next_node": "0000000000000012"},

        {"prev_node": "0000000000000009","next_node": "0000000000000010"},

        {"prev_node": "0000000000000010","next_node": "0000000000000011"},

        {"prev_node": "0000000000000014","next_node": "0000000000000015"},

        {"prev_node": "0000000000000015","next_node": "0000000000000016"},

        {"prev_node": "0000000000000016","next_node": "0000000000000017"},

        {"prev_node": "0000000000000018","next_node": "0000000000000019"},

        {"prev_node": "0000000000000020","next_node": "0000000000000021"},

        {"prev_node": "0000000000000019","next_node": "0000000000000020"},

        {"prev_node": "0000000000000012","next_node": "0000000000000022"},

        {"prev_node": "0000000000000022","next_node": "0000000000000023"},

        {"prev_node": "0000000000000023","next_node": "0000000000000009"},

        {"prev_node": "0000000000000011","next_node": "0000000000000024"},

        {"prev_node": "0000000000000024","next_node": "0000000000000014"},

        {"prev_node": "0000000000000017","next_node": "0000000000000025"},

        {"prev_node": "0000000000000025","next_node": "0000000000000018"},

        {"prev_node": "0000000000000007","next_node": "0000000000000021"},

        {"prev_node": None, "next_node": "0000000000000005"},

        {"prev_node": "0000000000000021", "next_node": None}

    ]

    graph = build_graph(nodes)

    branches, nodes = travel('root', graph)


    print("branch num: %d" % branches)

    print("max depth branch:")

    for i in nodes[::-1]:

        print(i)


branch num: 2

max depth branch:

root

0000000000000005

0000000000000006

0000000000000008

0000000000000012

0000000000000022

0000000000000023

0000000000000009

0000000000000010

0000000000000011

0000000000000024

0000000000000014

0000000000000015

0000000000000016

0000000000000017

0000000000000025

0000000000000018

0000000000000019

0000000000000020

0000000000000021


查看完整回答
反對(duì) 回復(fù) 2018-12-15
  • 1 回答
  • 0 關(guān)注
  • 591 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)