Directory structure (single-level, two-level, tree, acyclic graph) in Operating Systems - Time & Space Complexity
When working with directory structures, it is important to understand how the time to find or manage files grows as the number of files and folders increases.
We want to know how the structure type affects the time it takes to access or organize files.
Analyze the time complexity of searching for a file in different directory structures.
// Example: Searching for a file in a directory
function searchFile(directory, filename) {
for (let file of directory.files) {
if (file.name === filename) {
return file;
}
}
return null;
}
This code searches through all files in a directory to find a file with a matching name.
Look at what repeats when searching for a file:
- Primary operation: Checking each file's name in the directory.
- How many times: Once for every file in the directory.
As the number of files grows, the search takes longer because it checks more files one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of files; doubling files doubles the work.
Time Complexity: O(n)
This means the time to find a file grows in direct proportion to the number of files in the directory.
[X] Wrong: "Searching a file in any directory structure always takes the same time regardless of size."
[OK] Correct: Different directory structures organize files differently, so the time to find a file depends on how many files and folders must be checked.
Understanding how directory structures affect search time helps you explain file system design and efficiency clearly, a useful skill in many technical discussions.
What if the directory structure was a tree instead of a single-level list? How would the time complexity of searching for a file change?
Practice
Solution
Step 1: Understand single-level directory
In a single-level directory, all files are stored in one folder without any subfolders or hierarchy.Step 2: Compare with other structures
Two-level and tree structures have multiple folders or subfolders, and acyclic graph allows shared folders, so they are not single-level.Final Answer:
Single-level directory structure -> Option DQuick Check:
Single-level = all files in one folder [OK]
- Confusing single-level with two-level structure
- Thinking tree structure is single-level
- Assuming acyclic graph is single-level
Solution
Step 1: Recall two-level directory structure
In two-level structure, each user has their own directory under a common root directory.Step 2: Eliminate incorrect options
All files are stored in one directory without subdirectories describes single-level, B describes tree structure, and D describes cyclic graphs which are not allowed.Final Answer:
Each user has a separate directory under the root directory -> Option CQuick Check:
Two-level = user directories under root [OK]
- Mixing two-level with single-level structure
- Thinking two-level allows deep folder trees
- Confusing acyclic graph with two-level
Docs and Images. Docs contains files a.txt and b.txt. Images contains pic1.jpg. How many total files are in the directory tree?Solution
Step 1: Count files in Docs directory
Docs contains two files: a.txt and b.txt, so count is 2.Step 2: Count files in Images directory
Images contains one file: pic1.jpg, so count is 1.Step 3: Add all files
Total files = 2 (Docs) + 1 (Images) = 3 files.Final Answer:
3 -> Option AQuick Check:
2 + 1 = 3 files [OK]
- Counting directories as files
- Forgetting to add files from all subdirectories
- Miscounting number of files in Docs
Solution
Step 1: Understand tree and cycle concepts
A tree directory must not have cycles; linking back creates a cycle.Step 2: Identify consequences of cycle
Cycle causes infinite loops when traversing directories, breaking acyclic property.Final Answer:
The directory structure is no longer acyclic, causing infinite loops -> Option AQuick Check:
Cycle breaks acyclic tree causing infinite loops [OK]
- Thinking files become inaccessible immediately
- Assuming directory fixes cycle automatically
- Confusing cycle with single-level structure
Solution
Step 1: Understand acyclic graph directory
Acyclic graph allows multiple directories to share the same file via links, avoiding duplicates.Step 2: Identify advantage over tree structure
This sharing reduces disk space since only one copy of the file is stored with multiple references.Step 3: Eliminate incorrect options
Files can still be deleted, it does not force single directory storage, and cycles are not allowed.Final Answer:
It reduces disk space by storing one copy with multiple references -> Option BQuick Check:
Shared links save space in acyclic graph [OK]
- Thinking acyclic graph creates cycles
- Assuming files cannot be deleted
- Confusing with single-level directory
Ready to prove it?
Pass the test and this pattern moves unknown
