4.2.2

Graphs, Hash Tables & Stacks

Test yourself

Directed & Undirected Graphs

Graphs are data structures used to represent data items which have connections between them, such as locations on a map or products on an e-commerce website.

Illustrative background for GraphsIllustrative background for Graphs ?? "content

Graphs

  • Graphs consist of ‘nodes’ or ‘vertices’ which are joined by ‘arcs’ or ‘edges’ and can either be directed or undirected.
  • A directed graph is one where the connections between the nodes only go in one direction (the arcs are represented as arrows).
  • In an undirected graph, the connections are two-way.
Illustrative background for Graph traversalIllustrative background for Graph traversal ?? "content

Graph traversal

  • There are two methods of traversing a graph:
    • Depth-first.
    • Breadth-first.
Illustrative background for Depth-first traversalIllustrative background for Depth-first traversal ?? "content

Depth-first traversal

  • Depth-first traversal involves travelling down one route as far as possible, before reversing and travelling as far down the next route as possible.
  • In the graph shown, depth-first traversal would involve visiting the nodes in the following order:
    • A, B, E, C, D, G, F
Illustrative background for Breadth-firstIllustrative background for Breadth-first ?? "content

Breadth-first

  • Breadth-first traversal involves visiting all the adjoining nodes for each node in turn.
  • In the graph shown, breadth-first traversal would involve visiting the nodes in the following order:
    • A, B, C, E, D, F, G

Hash Tables

A hash table is a data structure used for very large collections of data or where quick access to the data is required, such as customer records or password verification.

Illustrative background for Hashing algorithmIllustrative background for Hashing algorithm ?? "content

Hashing algorithm

  • Hashing algorithms allows for the efficient location and retrieval of data.
  • The hashing algorithm generates indexes based on the key fields of sets of data.
  • A common example of a hashing algorithm is one which takes the key and divides it by the number of storage spaces available.
  • The remainder is then used as the address at which to store the key.
Illustrative background for ExampleIllustrative background for Example ?? "content

Example

  • If the keys 738, 271 and 560 need storing in a hash table with 6 storage spaces, the process would be as follows:
  • Calculate the remainder when 738 is divided by 6.
    • 738 MOD 6 = 0
  • That means first key should be stored in location 0.
Illustrative background for Example cont.Illustrative background for Example cont. ?? "content

Example cont.

  • For the second key, 271 MOD 6 = 1.
    • We store this in location 1.
  • For the third key, 560 MOD 6 = 2.
    • We store this in location 2.
Illustrative background for CollisionsIllustrative background for Collisions ?? "content

Collisions

  • A perfect hashing algorithm will always generate a unique index.
  • Should this not be the case, a ‘collision’ is said to occur .
  • When a collision occurs, a rehashing algorithm is used to determine the next available slot in which to store the data.
  • Alternatively, we can create a separate data structure such as a linked list from each storage location and store the collisions there.
Illustrative background for ExampleIllustrative background for Example ?? "content

Example

  • Key 362 would need adding to the hash table above in location 2 as:
    • 362 MOD 6 = 2
  • However, it’s already filled by 560.
  • In this case, the next available slot is location 3, so key 362 is stored there.

Stacks

A stack is a data structure where data items are always added and removed from the top. It is often referred to as a ‘last in, first out’ data structure.

Illustrative background for StacksIllustrative background for Stacks ?? "content

Stacks

  • A stack is a data structure where data items are always added and removed from the top. It is often referred to as a ‘last in, first out’ data structure.
  • Stacks can be implemented using other data structures, such as arrays or linked lists. In either case, an extra variable is required to identify the top of the stack.
Illustrative background for Stack operationsIllustrative background for Stack operations ?? "content

Stack operations

  • Several operations can be carried out on stacks:
    • push() - adds an item to the top of a stack.
    • pop() - removes and displays the item at the top of a stack
    • peek() - displays the item at the top of a stack without removing it
    • isEmpty() - checks to see if the stack is empty
    • isFull() - checks to see if the stack is full
Illustrative background for UsesIllustrative background for Uses ?? "content

Uses

  • The most common use of stacks is to reverse the order of a set of data items, as they are ‘popped’ in the opposite order that they were ‘pushed’ to a stack.
  • They are also used by processors to handle interrupts.
  • Stacks can be ‘static’ (fixed in size) or ‘dynamic’ (able to grow in size as necessary)
Illustrative background for ErrorsIllustrative background for Errors ?? "content

Errors

  • Stack overflow errors can occur in static stacks when attempting to push an item onto a stack that is full.
  • Similarly, when trying to pop from an empty stack a stack underflow error occurs.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Procedurally

6.3Thinking Logically

7Problem Solving & Programming

8Algorithms

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson