Tag Archives: DFS

📊 Graphs in Computer Science


🧩 What is a Graph?

Image
Image
Image

A graph is a powerful non-linear data structure used to represent relationships between objects. It consists of a set of vertices (nodes) and a set of edges (connections) that link pairs of vertices.

Graphs are used to model real-world systems such as:

  • Social networks
  • Transportation systems
  • Computer networks
  • Web page links

Formally, a graph is defined as:

G = (V, E)

Where:

  • V → Set of vertices
  • E → Set of edges

🧠 Key Terminology in Graphs

Image
Image
Image
Image
  • Vertex (Node) → Basic unit of a graph
  • Edge → Connection between vertices
  • Degree → Number of edges connected to a vertex
  • Path → Sequence of vertices connected by edges
  • Cycle → Path that starts and ends at the same vertex
  • Connected Graph → Every vertex is reachable
  • Disconnected Graph → Some vertices are isolated
  • Weighted Graph → Edges have weights/costs
  • Unweighted Graph → No weights on edges

🔗 Types of Graphs


🔹 1. Undirected Graph

Image
Image
Image
Image

Edges have no direction:

A — B

🔹 2. Directed Graph (Digraph)

Image
Image
Image
Image

Edges have direction:

A → B

🔹 3. Weighted Graph

Image
Image
Image
Image

Edges carry weights.


🔹 4. Unweighted Graph

Image
Image
Image

All edges are equal.


🔹 5. Cyclic Graph

Image
Image
Image
Image

Contains cycles.


🔹 6. Acyclic Graph

Image
Image
Image
Image

No cycles present.


🔹 7. Complete Graph

Image
Image
Image
Image

Every vertex connects to every other vertex.


🔹 8. Bipartite Graph

Image
Image
Image
Image

Vertices divided into two sets.


🔹 9. Sparse vs Dense Graph

Image
Image
Image
Image
  • Sparse → Few edges
  • Dense → Many edges

🧱 Graph Representation


🔹 1. Adjacency Matrix

Image
Image
Image
Image

2D matrix representation.


🔹 2. Adjacency List

Image
Image
Image
Image

Stores neighbors of each node.


⚙️ Graph Operations


🔹 1. Traversal

Image
Image
Image

DFS (Depth First Search)

  • Uses stack
  • Explores deep

BFS (Breadth First Search)

  • Uses queue
  • Explores level by level

🔹 2. Searching

Finding a node or path.


🔹 3. Insertion & Deletion

Adding/removing vertices or edges.


🧮 Graph Algorithms


🔹 1. Dijkstra’s Algorithm

Image
Image
Image
Image

Finds shortest path in weighted graphs.


🔹 2. Bellman-Ford Algorithm

Image
Image
Image

Handles negative weights.


🔹 3. Floyd-Warshall Algorithm

Image
Image
Image
Image

All-pairs shortest path.


🔹 4. Kruskal’s Algorithm

Image
Image
Image

Minimum spanning tree.


🔹 5. Prim’s Algorithm

Image
Image
Image
Image

Another MST algorithm.


🔹 6. Topological Sorting

Image
Image
Image
Image

Used in DAGs.


🧮 Time Complexity Overview

AlgorithmComplexity
BFSO(V + E)
DFSO(V + E)
DijkstraO((V+E) log V)
KruskalO(E log E)

⚡ Advantages of Graphs

  • Flexible structure
  • Models real-world relationships
  • Supports complex algorithms
  • Scalable

⚠️ Disadvantages of Graphs

  • Complex implementation
  • High memory usage
  • Difficult debugging

🧠 Advanced Graph Concepts


🔹 1. Strongly Connected Components

Image
Image
Image
Image

🔹 2. Network Flow

Image
Image
Image
Image

🔹 3. Graph Coloring

Image
Image
Image

🔹 4. Eulerian & Hamiltonian Paths

Image
Image
Image
Image

🔬 Applications of Graphs


🌐 1. Social Networks

Image
Image
Image
Image

🗺️ 2. GPS Navigation

Image
Image
Image
Image

🌍 3. Internet & Networking

Image
Image
Image
Image

🧠 4. Artificial Intelligence

Image
Image
Image
Image

🧬 5. Bioinformatics

Image
Image
Image
Image

🔁 Graph vs Tree

FeatureGraphTree
CyclesAllowedNot allowed
ConnectivityNot necessaryAlways connected
StructureGeneralHierarchical

🧪 Memory Representation

Graph stored as:

  • Matrix
  • List
  • Edge list

🚀 Real-World Importance

Graphs are essential in:

  • Machine learning
  • Networking
  • Logistics
  • Game development
  • Data science

🧾 Conclusion

Graphs are among the most powerful and flexible data structures in computer science. They allow representation of complex relationships and are the foundation of many advanced algorithms.

Mastering graphs is crucial for solving real-world problems, especially in areas like AI, networking, and optimization.


🏷️ Tags

🌳 Trees in Computer Science


🧩 What is a Tree Data Structure?

Image
Image
Image
Image

A tree is a widely used non-linear data structure that represents hierarchical relationships between elements. Unlike linear structures such as arrays, stacks, and queues, trees organize data in a branching structure resembling an inverted tree.

In a tree:

  • The topmost node is called the root
  • Each node may have child nodes
  • Nodes are connected by edges
  • There is exactly one path between any two nodes

Trees are fundamental in computer science and are used in databases, file systems, artificial intelligence, networking, and more.


🧠 Key Terminology in Trees

Image
Image
Image
Image

Understanding trees requires familiarity with key terms:

  • Node → Basic unit of a tree
  • Root → Topmost node
  • Edge → Connection between nodes
  • Parent → Node with children
  • Child → Node derived from another node
  • Leaf Node → Node with no children
  • Internal Node → Node with at least one child
  • Height → Longest path from root to leaf
  • Depth → Distance from root
  • Subtree → A tree within a tree

🌲 Basic Structure of a Tree

        A
      / | \
     B  C  D
    / \     \
   E   F     G
  • A is root
  • B, C, D are children
  • E, F, G are leaf nodes

🔗 Types of Trees


🔹 1. General Tree

Image
Image
Image
Image

A general tree allows any number of children per node.


🔹 2. Binary Tree

Image
Image
Image
Image

Each node has at most two children:

  • Left child
  • Right child

🔹 3. Full Binary Tree

Image
Image
Image
Image

Every node has either:

  • 0 children OR
  • 2 children

🔹 4. Complete Binary Tree

Image
Image
Image
Image

All levels are filled except possibly the last, which is filled from left to right.


🔹 5. Perfect Binary Tree

Image
Image
Image
Image

All internal nodes have two children and all leaves are at the same level.


🔹 6. Binary Search Tree (BST)

Image
Image
Image
Image

Properties:

  • Left subtree → smaller values
  • Right subtree → larger values

🔹 7. AVL Tree (Self-Balancing Tree)

Image
Image
Image
Image

Maintains balance using rotations.


🔹 8. Red-Black Tree

Image
Image
Image
Image

A balanced tree with coloring rules.


🔹 9. Heap (Binary Heap)

Image
Image
Image
Image

Used in priority queues:

  • Max Heap
  • Min Heap

🔹 10. B-Tree

Image
Image
Image
Image

Used in databases and file systems.


🔹 11. Trie (Prefix Tree)

Image
Image
Image
Image

Used for string searching and auto-complete.


⚙️ Tree Operations


🔹 1. Insertion

Image
Image
Image
Image

Adding nodes while maintaining properties.


🔹 2. Deletion

Image
Image
Image
Image

Cases:

  • Leaf node
  • One child
  • Two children

🔹 3. Searching

def search(root, key):
    if root is None or root.value == key:
        return root
    if key < root.value:
        return search(root.left, key)
    return search(root.right, key)

🔄 Tree Traversal Techniques

Image
Image
Image
Image

🔹 Depth First Search (DFS)

  • Inorder (Left, Root, Right)
  • Preorder (Root, Left, Right)
  • Postorder (Left, Right, Root)

🔹 Breadth First Search (BFS)

  • Level-order traversal using queue

🧮 Time Complexity

OperationBST AverageBST Worst
SearchO(log n)O(n)
InsertO(log n)O(n)
DeleteO(log n)O(n)

Balanced trees maintain O(log n).


⚡ Advantages of Trees

  • Efficient searching and sorting
  • Hierarchical representation
  • Dynamic size
  • Flexible structure

⚠️ Disadvantages of Trees

  • Complex implementation
  • More memory usage (pointers)
  • Balancing overhead

🧠 Advanced Tree Concepts


🔹 1. Segment Tree

Image
Image
Image
Image

Used for range queries.


🔹 2. Fenwick Tree (Binary Indexed Tree)

Image
Image
Image

Efficient prefix sums.


🔹 3. Suffix Tree

Image
Image
Image

Used in string algorithms.


🔬 Applications of Trees


💻 1. File Systems

Image
Image
Image
Image

Directories organized as trees.


🌐 2. HTML DOM

Image
Image
Image
Image

Web pages structured as trees.


🧠 3. Artificial Intelligence

Image
Image
Image
Image

Decision trees in ML.


🎮 4. Game Development

Image
Image
Image
Image

Game decision trees.


📊 5. Databases

Image
Image

Indexing using B-Trees.


🔁 Tree vs Other Data Structures

FeatureTreeArrayLinked List
StructureHierarchicalLinearLinear
AccessModerateFastSlow
FlexibilityHighLowHigh

🧪 Memory Representation

Each node contains:

  • Data
  • Pointers to children

Example:

Node:
[Data | Left Pointer | Right Pointer]

🚀 Real-World Importance

Trees are essential in:

  • Databases
  • Operating systems
  • Networking
  • Artificial intelligence
  • Compilers

🧾 Conclusion

Trees are one of the most powerful and versatile data structures in computer science. Their hierarchical nature makes them suitable for a wide range of applications, from simple data organization to complex algorithms in AI and databases.

Mastering trees is a critical step toward becoming proficient in data structures and algorithms.


🏷️ Tags