IT212 Data Structure

Unit 3: Graphs and Trees

R Batzinger

2024-07-12

Course description

Primitive types, arrays, records,
string and string processing,
data representation in memory,
pointers and references, linked structures,
knowledge of hashing function,
use of stacks, queues, graphs and trees,
and strategies for choosing the right data structure.

Course details:

  • Time: Mon and Thu
  • Venue: PC302/1 PENTECOST (Maekhao Campus)
  • Credits: 5 0 5
  • INSTRUCTOR: A.Dr. Robert P. Batzinger
  • MIDTERM: 14 Mar 2025 TIME 09:00 - 11:00

Basic components

  • Node A container of data and connections

  • Edges A connection between nodes

  • Branches a multiple edges from a node (2 or more)

Definitions of connected systems

  • Chain

    • a set of notes linked sequencially from(/to) root.
    • single link only forward direction
    • double link is forward and backward
  • Tree

    • single root
    • each branch leads to another branch or a leaf
    • (no circuit or loops allowed) Mathematical Statements
  • Graph

    • A network of connects that can create loops
    • Possible to have multiple starting points and multiple endings
    • Could end up circling forever

Chain Example

Binary Tree Example

Binary Tree (Node view)

A Graph Example

Course Title: Data Structures

Course Description: This course provides a comprehensive introduction to fundamental data structures and their implementations. Students will gain a solid understanding of how data is organized and manipulated within computer systems, and how to select appropriate data structures for different problem-solving scenarios.

Course Outline:

Module 1: Introduction to Data Structures and Algorithms

  • Educational Goal: To introduce the fundamental concepts of data structures and algorithms, their importance in computer science, and the relationship between them.
  • Topics:
    • Definition of data structures and algorithms
    • Importance of data structures in computer science
    • Algorithm design and analysis (time and space complexity)
    • Abstract data types (ADTs)

Module 2: Primitive Data Types and Data Representation

  • Educational Goal: To understand the basic building blocks of data structures and how they are represented in computer memory.
  • Topics:
    • Primitive data types (integers, floating-point numbers, characters, booleans)
    • Data representation in memory (binary, hexadecimal, two’s complement)
    • Bitwise operations
    • Character encoding (ASCII, Unicode)

Module 3: Arrays and Strings

  • Educational Goal: To understand the concept of arrays, their different types, and how they are used to store and manipulate collections of data.
  • Topics:
    • Arrays (one-dimensional, multi-dimensional, jagged arrays)
    • Array operations (searching, sorting, insertion, deletion)
    • Strings as arrays of characters
    • String manipulation algorithms (substring, concatenation, searching)

Module 4: Records and Structures

  • Educational Goal: To understand how to group related data items together using records (or structures) and their applications.
  • Topics:
    • Records (or structures) as collections of different data types
    • Accessing and modifying fields within records
    • Applications of records (e.g., representing employee data, student records)
    • Unions (data structures that can hold values of different types)

Module 5: Pointers and References

  • Educational Goal: To understand the concept of pointers and references, their role in memory management, and their use in implementing dynamic data structures.
  • Topics:
    • Memory addresses and pointers
    • Pointer arithmetic and operations
    • Pass-by-reference vs. pass-by-value
    • Dynamic memory allocation (malloc, free)
    • Dangling pointers and memory leaks

Module 6: Linked Lists

  • Educational Goal: To understand the concept of linked lists, their different types, and their applications in various data structures.
  • Topics:
    • Singly linked lists
    • Doubly linked lists
    • Circular linked lists
    • Linked list operations (insertion, deletion, traversal)
    • Applications of linked lists (e.g., implementing stacks, queues)

Module 7: Stacks, Queues, and Trees

  • Educational Goal: To understand the concepts of stacks, queues, and trees, their characteristics, and their implementations using arrays and linked lists.
  • Topics:
    • Stacks (LIFO) and their applications (e.g., function calls, expression evaluation)
    • Queues (FIFO) and their applications (e.g., scheduling, breadth-first search)
    • Trees (binary trees, binary search trees)
    • Tree traversal algorithms (in-order, pre-order, post-order)

Module 8: Hashing and Graphs

  • Educational Goal: To understand the concepts of hashing, graph data structures, and their applications in various domains.
  • Topics:
    • Hashing functions and hash tables
    • Collision resolution techniques (chaining, open addressing)
    • Graphs (directed, undirected, weighted)
    • Graph traversal algorithms (depth-first search, breadth-first search)
    • Applications of graphs (e.g., social networks, transportation networks)

Teaching Methods:

  • Lectures
  • Hands-on coding exercises and assignments
  • Problem-solving sessions
  • Group projects
  • Class discussions and debates

Assessment:

  • Assignments (coding exercises, problem-solving assignments)
  • Quizzes and exams
  • Midterm exam
  • Final project (implementing a data structure or algorithm)
  • Class participation

Basic Cornerstone

Program = Algorithm + Data Structure

Balanced tree

  • All empty links are located on the last row
  • Capacity of a binary tree: \(b^n - 1\)
  • 3 rows of a binary tree: \(2^3 - 1\)
Row No. Binary tree Teritary tree 4th tree
1 1 1 1
2 3 4 5
3 7 13 21
4 15 40 85

Recursive printing of a Binary tree

def printtree(root)
   if !root.leftlink.nil? 
        printtree(root.leftlink)
     end
     puts root.data
     if !root.rightlink.nil?
       printtree(root.leftlink)
     end
end

Some Examples

Isomers

  • same number of edges and nodes
  • same set of edges

Isomeric Graph A

n1 n2 n3 n4 n5
n1 e1 e6
n2 e1 e2 e5
n3 e2 e3
n4 e6 e3 e4
n5 e5 e4

Isomeric Graph B

n1 n2 n3 n4 n5
n1 e1 e6
n2 e1 e2 e5
n3 e2 e3
n4 e6 e3 e4
n5 e5 e4

Directed and Weighted graphs

Graphic rendering

Transition Matrices

Weighted, Directed Graph

From / To A B C D E F G
A 3
B 1
C 3 3
D 5
E 2 2 2
F 2
G 2

Unweighted, Directed Graph

From / To A B C D E F G
A 1
B 1
C 1 1
D 1
E 1 1 1
F 1
G 1

Undirected, Weighted

From / To A B C D E F G
A 1 3
B 1 5
C 3 3 3
D 5 3 2
E 3 2 2 2
F 2
G 2

Undirected, Unweighted

From / To A B C D E F G
A 1 1
B 1 1
C 1 1 1
D 1 1 1
E 1 1 1 1
F 1
G 1

Following the paths

One Step

  A B C D E F G
A 0 0 1 0 0 0 0
B 1 0 0 0 0 0 0
C 0 0 0 1 1 0 0
D 0 1 0 0 0 0 0
E 0 0 0 1 0 1 1
F 0 0 0 0 1 0 0
G 0 0 0 0 1 0 0

Two Steps

\[\tiny T=\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 0& 1& 1\\ 0& 0& 0& 0& 1& 0& 0\\ 0& 0& 0& 0& 1& 0& 0\\ \end{matrix}\right]\left[\begin{matrix} 0& 0& 1& 0& 0& 0& 0\\ 1& 0& 0& 0& 0& 0& 0\\ 0& 0& 0& 1& 1& 0& 0\\ 0& 1& 0& 0& 0& 0& 0\\ 0& 0& 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 1 2 0 2 2
B 1 1 0 0 2 0 0
C 0 2 1 1 5 0 0
D 0 1 0 1 0 1 1
E 2 0 0 5 1 4 4
F 0 2 1 0 4 0 0
G 0 2 1 0 4 0 0

Dijesktra Algorithm

Path A to G (Undirected,Unweighted)

A B C D E F G
* (A) (A) 0 0 0 0
0 (1) (1)
* A A B 0 0
0 1 (1) (2)
* A A B C 0 0
0 1 1 (2) (2)
* A A B C E E
0 1 1 2 2 (3) (3)
* A A B C E E
0 1 1 2 2 3 (3)
* A A B C E E
0 1 1 2 2 3 3
G -> E -> C -> A; Distance: 3 units 

Path A to G (Directed,Weighted)

A B C D E F G
* (A) 0 0 0 0
0 (3)
* A (C) (C) 0 0
0 3 (6) (6)
* (D) A C (C) 0 0
0 (11) 3 6 (6)
* (D) A C C (E) (E)
0 (11) 3 6 6 (8) (8)
* (D) A C C E (E)
0 (11) 3 6 6 8 (8)
* (D) A C C E E
0 (11) 3 6 6 8 8
* D A C C E E
0 11 3 6 6 8 8
 G -> E -> C -> A; 8 units

Google Maps

 Singapore -> Malaysia -> Burma ->
 Bhutan -> India -> Pakistan ->
 Afghanistan -> Tajikista -> Kyrgyzstan ->
 Uzbekistan -> Kazakhstan -> Russia ->
 Lithuania -> Poland -> Germany -> Netherlands ->
 United Kingdom

  • Distance 15,040
  • Time: 3,352 hr

Another Trip

  • Start: Raffles Hotel, Singapore

  • End: SeaPoint, Cape Town, South Africa

  • Distance: 20,392 km

  • Time:4,581 hr

Exercise

Given this graph:

  • determine the conductivity map
  • determine the shortest path from G to A

Hamilton Curcuits and Paths

  • Hamiltonian circuit: visits every vertex once with no repeats. Being a circuit, it must start and end at the same vertex. (All vertices have even number of edges.)

  • Hamiltonian path: visits every vertex once with no repeats, but does not start and end at the same vertex. (At most 2 vertices with odd number of edges.)

A Hamilton path

(Only works when you start and end on the vertices with odd number of edges. Only two or less vertices with odd numbered edges are permitted)

A hamilton circuit:

Works from any starting point and always ends up at the starting point.

Neither Hamilton Curcuit nor Path

More than 2 vertices with odd number of edges