markdown
スタックとキューの基本md 1530646
lecture/information/algorithm/data-structures/stack-and-queue-basics.lecture.n.md
Download PDF

スタックとキューの基本きほん

date2026-07-14document_iddoc_8f494f7c9e0522ea22b8df5937f1940cdescriptionスタックとキューを抽象データ型として定義し、操作契約、実装、計算量、DFSとBFSへの接続を説明する。prerequisites計算量の基本 / 配列の基本操作 / 抽象データ型と実装の区別type講義statusactiverelateddata/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md / data/lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md
algorithmdata-structuresundergraduatelecture
data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md

1導入どうにゅう

この講義こうぎでは、スタックとキューを抽象ちゅうしょうデータがたとして定義ていぎし、操作そうさ契約けいやくから構造こうぞうたいする挙動きょどう代表的だいひょうてき実装じっそう計算量けいさんりょう説明せつめいする。さらに、両者りょうしゃ取出順序とりだしじゅんじょがDFSとBFSの探索順序たんさくじゅんじょ規定きていすることを確認かくにんする。

2スタックの契約けいやく

スタック(stack)は、最後さいご追加ついかした要素ようそ最初さいしょ除去じょきょするLIFO(last in, first out)のADTである。基本操作きほんそうさつぎのようにさだめる。

  • push(x)要素ようそ x頂上ちょうじょう追加ついかする。
  • pop()頂上ちょうじょう要素ようそ除去じょきょしてかえす。
  • top()頂上ちょうじょう要素ようそ除去じょきょせずにかえす。
  • isEmpty()要素数ようそすう0 であるかをかえす。

からのスタックに pop() または top()適用てきようした結果けっかは、ADTの契約けいやく明示めいじする必要ひつようがある。本講義ほんこうぎでは、この操作そうさ事前条件違反じぜんじょうけんいはんとし、呼出側よびだしがわisEmpty() によって回避かいひする。

push(a), push(b), pop()じゅん実行じっこうすると、pop()bかえし、aのこる。

3キューの契約けいやく

キュー(queue)は、最初さいしょ追加ついかした要素ようそ最初さいしょ除去じょきょするFIFO(first in, first out)のADTである。追加側ついかがわ末尾まつび除去側じょきょがわ先頭せんとうぶ。

  • enqueue(x)x末尾まつび追加ついかする。
  • dequeue()先頭せんとう要素ようそ除去じょきょしてかえす。
  • front()先頭せんとう要素ようそ除去じょきょせずにかえす。
  • isEmpty()要素数ようそすう0 であるかをかえす。

からのキューに dequeue() または front()適用てきようすることも事前条件違反じぜんじょうけんいはんとする。enqueue(a), enqueue(b), dequeue()じゅん実行じっこうすると、dequeue()aかえし、bのこる。

4実装じっそう計算量けいさんりょう

スタックは動的配列どうてきはいれつ末尾まつび頂上ちょうじょうとすれば実装じっそうできる。容量ようりょう定数倍ていすうばい拡張かくちょうする動的配列どうてきはいれつでは、push償却しょうきゃく O(1)poptopisEmpty最悪時さいあくじ O(1) である。固定容量配列こていようりょうはいれつでは、満杯時まんぱいじ挙動きょどう契約けいやく追加ついかする必要ひつようがある。

キューは循環配列じゅんかんはいれつ先頭せんとう末尾まつび添字そえじ、または両端りょうたんへの参照さんしょうをもつ連結れんけつリストで実装じっそうできる。固定容量こていようりょう循環配列じゅんかんはいれつでは、満杯まんぱいでないことを enqueue事前条件じぜんじょうけんとすれば、すべての操作そうさ最悪時さいあくじ O(1) である。容量ようりょう定数倍ていすうばい拡張かくちょうする循環配列じゅんかんはいれつでは、enqueue償却しょうきゃく O(1) であるが、拡張時かくちょうじ最悪時間さいあくじかんO(n) である。連結れんけつリストでは、要素ようそ割当わりあてO(1) とする計算模型けいさんもけいもとで、すべての操作そうさ最悪時さいあくじ O(1) となる。ただし、通常つうじょう配列はいれつdequeue のたびに全要素ぜんようそひだり移動いどうする実装じっそうO(n) 時間じかんようする。したがって、O(1) という評価ひょうかはADT自体じたいではなく、選択せんたくした実装じっそう依存いぞんする。

いずれも n 要素ようそ保持ほじする領域りょういきO(n) である。

5DFSとBFSへの接続せつぞく

ふか優先探索ゆうせんたんさく(DFS)は、発見済はっけんずみで未処理みしょり頂点ちょうてんをスタックで管理かんりする。最後さいご追加ついかした候補こうほ優先ゆうせんするため、一方向いちほうこう探索たんさく継続けいぞくしてから後戻あともどりする。再帰実装さいきじっそうでは、関数呼出かんすうよびだしのスタックがこの役割やくわりになう。

幅優先探索はばゆうせんたんさく(BFS)は候補頂点こうほちょうてんをキューで管理かんりする。さき発見はっけんした候補こうほさき処理しょりするため、始点してんからの探索たんさくレベルが非減少順ひげんしょうじゅんになる。このFIFO順序じゅんじょが、無重むおもみグラフにおけるBFSの最短距離計算さいたんきょりけいさんささえる。

data/lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md

6まとめ

  • スタックはLIFO、キューはFIFOの操作契約そうさけいやくをもつADTである。
  • 空構造くうこうぞうへの除去操作じょきょそうさ参照操作さんしょうそうさ挙動きょどうは、契約けいやく明示めいじする。
  • 操作そうさ計算量けいさんりょう具体的ぐたいてき実装じっそう依存いぞんする。
  • DFSはスタック、BFSはキューによって候補こうほ管理かんりする。

Stack and Queue Basics

data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md

1Introduction

This lecture defines stacks and queues as abstract data types and explains their operation contracts, behavior on empty structures, representative implementations, and complexity. It then shows how their removal orders determine the traversal order of DFS and BFS.

2Stack contract

A stack is a last-in, first-out (LIFO) ADT: the most recently inserted element is removed first. Its basic operations are as follows.

  • push(x) inserts x at the top.
  • pop() removes and returns the top element.
  • top() returns the top element without removing it.
  • isEmpty() reports whether the number of elements is zero.

The contract must specify the result of applying pop() or top() to an empty stack. In this lecture, either operation on an empty stack violates a precondition, and the caller must prevent it with isEmpty().

After push(a), push(b), and pop(), in that order, pop() returns b and a remains.

3Queue contract

A queue is a first-in, first-out (FIFO) ADT: the earliest inserted element is removed first. The insertion side is the rear, and the removal side is the front.

  • enqueue(x) inserts x at the rear.
  • dequeue() removes and returns the front element.
  • front() returns the front element without removing it.
  • isEmpty() reports whether the number of elements is zero.

Applying dequeue() or front() to an empty queue also violates a precondition. After enqueue(a), enqueue(b), and dequeue(), in that order, dequeue() returns a and b remains.

4Implementations and complexity

A stack can use the end of a dynamic array as its top. With geometric capacity growth, push takes amortized O(1) time, while pop, top, and isEmpty take worst-case O(1) time. A fixed-capacity array requires the contract to specify behavior when the array is full.

A queue can use a circular array with front and rear indices or a linked list with references to both ends. In a fixed-capacity circular array, all operations take worst-case O(1) time if enqueue requires that the array is not full. In a circular array whose capacity grows geometrically, enqueue takes amortized O(1) time but O(n) time in a resizing operation. In a linked list, all operations take worst-case O(1) time under a computational model in which allocating one node takes O(1) time. In contrast, an ordinary array implementation that shifts every element left after each dequeue takes O(n) time. Thus, an O(1) bound belongs to a chosen implementation, not to the ADT itself.

Both structures require O(n) space to store n elements.

5Connection to DFS and BFS

Depth-first search (DFS) manages discovered but unprocessed vertices with a stack. Prioritizing the most recently added candidate continues traversal in one direction before backtracking. In a recursive implementation, the function-call stack serves this purpose.

Breadth-first search (BFS) manages candidate vertices with a queue. Processing earlier discoveries first makes traversal levels from the start vertex nondecreasing. This FIFO order supports the shortest-distance property of BFS in an unweighted graph.

data/lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md

6Summary

  • A stack is a LIFO ADT, and a queue is a FIFO ADT.
  • The contract must specify behavior of removal and access operations on an empty structure.
  • Operation complexity depends on the concrete implementation.
  • DFS manages candidates with a stack, whereas BFS uses a queue.
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる