ASSOCIATIVE
ARRAY & GRAPH
WHAT IS ASSOCIATIVE ARRAY:
An associative array, also known as a map, dictionary, or hash table in various programming
languages, is a data structure that stores key-value pairs. It allows you to associate a unique
key with a specific value and retrieve, update, or delete the value using the key.
Definitions:
1. Map: An interface in Java that maps unique keys to
values.
2. Key: A unique identifier for a value in the map.
3. Value: The data associated with a key.
Functions:
Insertion: Add a new key-value pair or update an existing
key's value.
Deletion: Remove a key-value pair using the key.
Lookup: Retrieve the value associated with a specific
key.
Update: Change the value associated with a particular
key.
// Declare an associative array
Example in Java and in (dictionary/map)
ASSOCIATIVE_ARRAY myMap
PSUEDO CODE:
// Add key-value pairs to the associative
Here’s an example using array
myMap["Alice"] = 30
Hash Map, a commonly used myMap["Bob"] = 25
implementation of the Map myMap["Charlie"] = 35
interface // Retrieve a value based on a key
value = myMap["Alice"]
PRINT "Alice's age: " + value
// Check if a key exists in the associative
array
IF "Alice" IN myMap THEN
PRINT "Alice is in the map."
ENDIF
// Remove a key-value pair from the
associative array
REMOVE myMap["Bob"]
// Print the contents of the associative array
FOR EACH key IN myMap
PRINT key + ": " + myMap[key]
Explanation:
Key-Value Pair: Each entry in a map
consists of a key and its associated
value.
Uniqueness: Each key must be unique;
however, multiple keys can map to the
same value.
Efficiency: Different implementations of
the Map interface offer various
performance characteristics for these
operations.
WHAT IS GRAPH:
A graph is a data structure used to represent relationships or connections between entities. It
consists of a set of nodes (also called vertices) and a set of edges that connect pairs of nodes.
Graphs are widely used in computer science to model various real-world problems and
relationships.
Definitions:
Graph: A collection of vertices (nodes) and edges
(connections) that can be either directed or undirected.
Vertex: A node in the graph representing an entity.
Edge: A connection between two vertices. In a directed
graph, edges have a direction; in an undirected graph,
they do not.
Functions:
Add Vertex: Insert a new vertex into the graph.
Add Edge: Insert a connection between two vertices.
Remove Vertex: Remove a vertex and its associated
edges from the graph.
Remove Edge: Remove a connection between two
vertices.
Traversal: Explore the graph using methods like Depth-
First Search (DFS) or Breadth-First Search (BFS).
Here's an example of a
simple directed &
undirected graph
implementation using an
adjacency list:
Explanation of the Example:
Adjacency List: Uses a Hash Map to store vertices and their
connected vertices.
Add Vertex: Adds a vertex with an empty list of edges.
Add Edge: Adds an edge between two vertices, updating both lists
to maintain undirected connectivity.
Remove Vertex: Removes a vertex and its connections from other
vertices' lists.
Remove Edge: Removes an edge between two vertices.
Print Graph: Displays each vertex and its connected vertices.
1 - 3 What are the 3 definitions in associative array.
4 - 6 What are the 3 definitions in graph.
7. what is associative array.
8. what is graph.
9 – 11 what are the different functions of associative array. Give at least 3 functions.
12 – 15 what are the different functions of graph. Give at least 4 functions.
Test II: Hands-on
Create an associative array where String keys
NAME are mapped to integers value of AGE.
Put adds key-value pairs to the map. Get
retrieves a value based on a key. Key set is
use to loop through all keys in map.