포도먹고싶다.
그 그래프 아니고 Graph
def search_number(grape, num):
todo = []
done = []
todo.append(num)
done.append(num)
print(num)
while(todo):
current = todo.pop(0)
for item in grape[current]:
print(current,item)
if item not in done:
todo.append(item)
done.append(item)
return done
search_number({
1:[2,3],
2:[1,4,5],
3:[1],
4:[2],
5:[2]
},1)
# 친밀도
# def print_all_friend(g, name):
# qu = []
# done = {}
# qu.append(name)
# done[name] = 1
# while(qu):
# current = qu.pop(0)
# for value in g[current]:
# if value not in done:
# qu.append(value)
# done[value] = 1
# else:
# done[value] += 0.8
# return done
# 촌수확인
def print_all_friend(group, name):
toSearch = []
done = {}
toSearch.append((name,0))
done[name] = 0
while(toSearch):
(p,d) = toSearch.pop(0)
for item in group[p]:
if item not in done:
done[item] = d + 1
toSearch.append((item,d+1))
return done
friends = {
"Summer":["John","Justin","Mike"],
"John":["Summer","Justin"],
"Justin":["Summer","John","Mike","May"],
"Mike":["Summer","Justin"],
"May":["Justin","Kim"],
"Kim":["May"],
"Tom":["Jerry"],
"Jerry":["Tom"]
}
print(print_all_friend(friends, "Summer"))
print(print_all_friend(friends, "Tom"))
print(print_all_friend(friends, "Mike"))