广度优先搜索和深度优先搜索算法对背包问题的比较

huangapple go评论77阅读模式
英文:

Comparison of BFS and DFS algorithm for the Knapsack problem

问题

I have translated the content you provided. Here it is:

我对Python还比较新,有一个任务要求我比较算法的时间消耗和内存使用情况。

我已经编写了这两个算法并运行了它们。我可以测量时间的使用,但无法找到方法来知道使用了多少内存。我也不确定问题是要求我基于常规的BFS和DFS计算,还是基于我编写的代码。

比较算法的时间消耗。

比较算法在某一时刻的内存使用情况。

为了获取时间,我使用了 start_time = time.time()end = time.time()

BFS算法
0.0060007572174072266秒
DFS算法
0.005002260208129883秒

请问我应该如何计算内存使用情况,假设它是基于我的代码的。也许我有点困惑,但问题的措辞让我觉得需要在运行两个算法时测量内存使用情况以比较性能。


代码:

BFS:

def knapsack_bfs(items, max_weight):
    queue = deque()
    root = Node(-1, 0, 0, [])
    queue.append(root)

    max_benefit = 0
    best_combination = []

    while queue:
        current = queue.popleft()

        if current.level == len(items) - 1:
            if current.benefit > max_benefit:
                max_benefit = current.benefit
                best_combination = current.items
        else:
            next_level = current.level + 1
            next_item = items[next_level]

            include_benefit = current.benefit + next_item.benefit
            include_weight = current.weight + next_item.weight

            if include_weight <= max_weight:
                include_node = Node(next_level, include_benefit,
                                    include_weight, current.items + [next_item.id])
                if include_benefit > max_benefit:
                    max_benefit = include_benefit
                    best_combination = include_node.items
                queue.append(include_node)

            exclude_node = Node(next_level, current.benefit,
                                current.weight, current.items)
            queue.append(exclude_node)

    return max_benefit, best_combination

DFS:

def knapsack_dfs(items, max_weight):
    queue = []
    root = Node(-1, 0, 0, [])
    queue.append(root)

    max_benefit = 0
    best_combination = []

    while queue:
        current = queue.pop()

        if current.level == len(items) - 1:
            if current.benefit > max_benefit:
                max_benefit = current.benefit
                best_combination = current.items
        else:
            next_level = current.level + 1
            next_item = items[next_level]

            include_benefit = current.benefit + next_item.benefit
            include_weight = current.weight + next_item.weight

            if include_weight <= max_weight:
                include_node = Node(next_level, include_benefit,
                                    include_weight, current.items + [next_item.id])
                if include_benefit > max_benefit:
                    max_benefit = include_benefit
                    best_combination = include_node.items
                queue.append(include_node)

            exclude_node = Node(next_level, current.benefit,
                                current.weight, current.items)

            queue.append(exclude_node)

    return max_benefit, best_combination

编辑:

基于下面的回答,以下是结果:

program.py:42: size=4432 B (+840 B), count=79 (+15), average=56 B
program.py:116: size=0 B (-768 B), count=0 (-1)
program.py:79: size=0 B (-744 B), count=0 (-13)
program.py.py:85: size=0 B (-72 B), count=0 (-1)
program.py:57: size=0 B (-56 B), count=0 (-1)
program.py:56: size=0 B (-56 B), count=0 (-1)
program.py:74: size=0 B (-32 B), count=0 (-1)
program.py:37: size=32 B (+0 B), count=1 (+0), average=32 B
英文:

I am fairly new to python and I have a task which tells me to compare both algorithm time expended and space used in memory.

I have coded both algorithms and ran them both. I was able to measure the time used, but wasnt able to look for ways to know how much space was used. I am also not sure if the question is asking me to calculate it based on general BFS and DFS or the code I have coded.

> Comparison of the time expended by the algorithms.
>
> Comparison of the space used in memory at a time by the algorithms

To get the time I used start_time = time.time() and end = time.time()

BFS algorithm
0.0060007572174072266s 
DFS algorithm
0.005002260208129883s

How would I calculate the space used in memory assuming that it is based on my code. I might be just confused but the wording of the question makes me feel like I need to measure it when running both algorithms to compare the performance.


Code:

BFS :

def knapsack_bfs(items, max_weight):
    queue = deque()
    root = Node(-1, 0, 0, [])
    queue.append(root)

    max_benefit = 0
    best_combination = []

    while queue:
        current = queue.popleft()

        if current.level == len(items) - 1:
            if current.benefit &gt; max_benefit:
                max_benefit = current.benefit
                best_combination = current.items
        else:
            next_level = current.level + 1
            next_item = items[next_level]

            include_benefit = current.benefit + next_item.benefit
            include_weight = current.weight + next_item.weight

            if include_weight &lt;= max_weight:
                include_node = Node(next_level, include_benefit,
                                    include_weight, current.items + [next_item.id])
                if include_benefit &gt; max_benefit:
                    max_benefit = include_benefit
                    best_combination = include_node.items
                queue.append(include_node)

            exclude_node = Node(next_level, current.benefit,
                                current.weight, current.items)
            queue.append(exclude_node)

    return max_benefit, best_combination

DFS:

def knapsack_dfs(items, max_weight):
    queue = []
    root = Node(-1, 0, 0, [])
    queue.append(root)

    max_benefit = 0
    best_combination = []

    while queue:
        current = queue.pop()

        if current.level == len(items) - 1:
            if current.benefit &gt; max_benefit:
                max_benefit = current.benefit
                best_combination = current.items
        else:
            next_level = current.level + 1
            next_item = items[next_level]

            include_benefit = current.benefit + next_item.benefit
            include_weight = current.weight + next_item.weight

            if include_weight &lt;= max_weight:
                include_node = Node(next_level, include_benefit,
                                    include_weight, current.items + [next_item.id])
                if include_benefit &gt; max_benefit:
                    max_benefit = include_benefit
                    best_combination = include_node.items
                queue.append(include_node)

            exclude_node = Node(next_level, current.benefit,
                                current.weight, current.items)
            
            queue.append(exclude_node)

    return max_benefit, best_combination

Edit:

Results based on the answer below:

program.py:42: size=4432 B (+840 B), count=79 (+15), average=56 B
program.py:116: size=0 B (-768 B), count=0 (-1)
program.py:79: size=0 B (-744 B), count=0 (-13)
program.py.py:85: size=0 B (-72 B), count=0 (-1)
program.py:57: size=0 B (-56 B), count=0 (-1)
program.py:56: size=0 B (-56 B), count=0 (-1)
program.py:74: size=0 B (-32 B), count=0 (-1)
program.py:37: size=32 B (+0 B), count=1 (+0), average=32 B

答案1

得分: 1

你可以尝试按照另一个答案建议的方法操作: https://stackoverflow.com/a/45679009/670693

尽管我无法运行您的代码以尝试事情,缺少 Node 定义,但您可以尝试类似以下的操作:

import tracemalloc

tracemalloc.start()
knapsack_dfs(...)
dfs_snapshot = tracemalloc.take_snapshot()
knapsack_bfs(...)
bfs_snapshot = tracemalloc.take_snapshot()
tracemalloc.stop()

bfs_snapshot = bfs_snapshot.filter_traces((tracemalloc.Filter(False, '*tracemalloc.py'),))
dfs_snapshot = dfs_snapshot.filter_traces((tracemalloc.Filter(False, '*tracemalloc.py'),))

for statdiff in bfs_snapshot.compare_to(dfs_snapshot, 'lineno'):
  print(statdiff)
英文:

You can try doing what another answer suggested: https://stackoverflow.com/a/45679009/670693

While I couldn't run your code to try things out, missing the Node definition, you can try something like:

import tracemalloc

tracemalloc.start()
knapsack_dfs(...)
dfs_snapshot = tracemalloc.take_snapshot()
knapsack_bfs(...)
bfs_snapshot = tracemalloc.take_snapshot()
tracemalloc.stop()

bfs_snapshot = bfs_snapshot.filter_traces((tracemalloc.Filter(False, &#39;*tracemalloc.py&#39;),))
dfs_snapshot = dfs_snapshot.filter_traces((tracemalloc.Filter(False, &#39;*tracemalloc.py&#39;),))

for statdiff in bfs_snapshot.compare_to(dfs_snapshot, &#39;lineno&#39;):
  print(statdiff)

huangapple
  • 本文由 发表于 2023年5月22日 04:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定