はじめに

Pythonで学ぶデータ構造とアルゴリズム

Miriam Antona

Software Engineer

アルゴリズムとデータ構造の重要性

  • データ構造とアルゴリズムでできること
    • 日常の問題を解決する
    • 効率的なコーディング
  • 本コースはお好きなプログラミング言語で応用可能
Pythonで学ぶデータ構造とアルゴリズム

アルゴリズムとデータ構造

  • アルゴリズム:問題を解くための一連の手順

    1. 設計

      アルゴリズム設計の模式図。

    2. コード化

      アルゴリズムのコードの模式図。

  • データ構造: アルゴリズムを実行する際にデータを保持・操作する仕組み
    • 高度なデータ構造:連結リスト、スタック、キューなど
Pythonで学ぶデータ構造とアルゴリズム

連結リスト

パンを作る工程を表した連結リストの視覚的な例。

  • リンクでつながったデータの並び
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

連結リスト内のノード

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

連結リスト内のノードの表現。最初の部分に「データ」という語がある。

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

連結リスト内のノード。最初の部分に「DATA」という語があり、2番目の部分にポインタ付きの「NEXT」がある。

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

リンクで接続された連結リストの2つのノード

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

リンクで接続された連結リストの複数のノード

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

最後のノードがnullを指す連結リスト

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

最初のノードに「HEAD」という語がある連結リスト。

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 構造

最後のノードに「TAIL」という語がある連結リスト

  • データはメモリ上で連続した場所に置く必要がない
  • データは利用可能なメモリアドレスがあればどこでも配置できる
Pythonで学ぶデータ構造とアルゴリズム

単方向連結リスト

単方向連結リスト

  • リンクが1つ:単方向連結リスト
Pythonで学ぶデータ構造とアルゴリズム

双方向連結リスト

双方向連結リスト

  • 両方向にリンクがある:双方向連結リスト
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 実際の用途

  • 他のデータ構造の構築:
    • スタック
    • キュー
    • グラフ
  • 前後に移動しながら情報にアクセスする操作
    • ウェブブラウザー
    • 音楽プレイリスト
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - Nodeクラス

class Node:
  def __init__(self, data):
    self.data = data
    self.next = None
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - LinkedListクラス

class LinkedList:
  def __init__(self):
    self.head = None
    self.tail = None
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - メソッド

  • insert_at_beginning()
  • remove_at_beginning()
  • insert_at_end()
  • remove_at_end()
  • insert_at()
  • remove_at()
  • search()
  • ...
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - insert_at_beginning

単方向連結リスト

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - insert_at_beginning

単方向連結リストと新しいノード

 def insert_at_beginning(self, data):
    new_node = Node(data)

if self.head:
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - insert_at_beginning

新しいノードが接続された単方向連結リストの図。

 def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head:
      new_node.next = self.head

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - insert_at_beginning

新しいノードが接続され、このノード内に「HEAD」という語がある単方向連結リスト

 def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head:
      new_node.next = self.head
      self.head = new_node

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - insert_at_beginning

空の連結リストにおける新しいノード

 def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head:
      new_node.next = self.head
      self.head = new_node
    else:

self.tail = new_node self.head = new_node
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - insert_at_end

  def insert_at_end(self, data):
    new_node = Node(data)
    if self.head:  

self.tail.next = new_node
self.tail = new_node
else:
self.head = new_node self.tail = new_node
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - search

def search(self, data):

current_node = self.head
while current_node:
if current_node.data == data:
return True

単方向連結リストの表現で、"current_node" という語が最初のノードを指している

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - search

def search(self, data):
  current_node = self.head
  while current_node:
    if current_node.data == data:
      return True
    else:
      current_node = current_node.next

単方向連結リストの表現。`current_node` という語が2番目のノードを指している。

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - search

def search(self, data):
  current_node = self.head
  while current_node:
    if current_node.data == data:
      return True
    else:
      current_node = current_node.next

return False

単方向連結リストの表現。単語「current_node」が3番目のノードを指している。

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 例

空の連結リストにおける新しいノード

sushi_preparation = LinkedList()

sushi_preparation.insert_at_end("prepare")
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 例

連結リストの末尾に追加された新しいノード

sushi_preparation = LinkedList()  
sushi_preparation.insert_at_end("prepare")
sushi_preparation.insert_at_end("roll") 

Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 例

連結リストの先頭に追加された新しいノード

sushi_preparation = LinkedList()  
sushi_preparation.insert_at_end("prepare")
sushi_preparation.insert_at_end("roll") 
sushi_preparation.insert_at_beginning("assemble")
Pythonで学ぶデータ構造とアルゴリズム

連結リスト - 例

連結リスト

sushi_preparation.search("roll")
True
sushi_preparation.search("mixing")
False
Pythonで学ぶデータ構造とアルゴリズム

練習しましょう!

Pythonで学ぶデータ構造とアルゴリズム

Preparing Video For Download...