Graph Terminology In Python

Introduction to Graphs Terminology In Python

Graphs, pivotal in computer science and mathematics, are dynamic structures representing relationships among entities. Widely applied in social networks, transportation, and computer systems, they offer versatility.

This Python exploration delves into graph basics – types, representation, and algorithms. Starting with key terminology: nodes (entities) and edges (connections). Graphs vary – directed/undirected, weighted/unweighted, cyclic/acyclic. Representations include adjacency matrices/lists.

Overview Of Graph

Graph Definitions:

  1. Vertices (Nodes): These are the fundamental entities within a graph, representing points or objects.
  2. Edges: Edges connect vertices, illustrating relationships or connections between them.
  3. Paths: A path is a sequence of vertices where each adjacent pair is connected by an edge, showcasing a route through the graph.
  4. Cycles: A cycle is a path that begins and ends at the same vertex, forming a closed loop.
  5. Directed Graph: In a directed graph, edges have a direction, indicating a one-way connection between vertices.
  6. Undirected Graph: In an undirected graph, edges have no direction, representing a mutual connection between vertices.
  7. Weighted Graph: Edges in a weighted graph have associated numerical values, denoting a weight or cost.
  8. Unweighted Graph: In contrast, unweighted graphs have no numerical assignments to their edges.

Graph Representations:

 

Adjacency Matrix: A 2D array where each cell (i, j) indicates an edge between vertices i and j. Efficient for dense graphs but memory-intensive for sparse ones.

Example:


   0  1  2
0 [0, 1, 1]
1 [1, 0, 0]
2 [1, 0, 0]

Adjacency List: A collection where each vertex maintains a list of neighboring vertices. Efficient for sparse graphs, requiring less memory.

Example:


{
   0: [1, 2],
   1: [0],
   2: [0]
}

These representations are fundamental for graph understanding in computer science and mathematics.

Motivation

Numerous algorithms leverage graph representations to model data or problem-solving scenarios effectively. Here are a few examples illustrating the diverse applications of graph structures:
 
  1. Cities with Distances Between:
      • Representation: Graphs can model cities as vertices, and the distances between them as edges.
      • Application: This representation is instrumental in solving problems related to route optimization and logistics.

     

  2. Roads with Distances Between Intersection Points:
    • Representation: Intersections become vertices, and road segments are depicted as edges with associated distances.
    • Application: Facilitates the analysis of transportation networks, aiding in traffic flow optimization and route planning.
  3. Course Prerequisites:
    • Representation: Courses are nodes, and directed edges signify prerequisites.
    • Application: Helps in determining the optimal sequence of courses considering prerequisite dependencies in academic settings.
  4. Networks:
    • Representation: Devices or nodes connected by communication links as edges.
    • Application: Essential for analyzing and optimizing data communication networks, ensuring efficient data transfer.
  5. Social Networks:
    • Representation: Individuals as nodes, with edges denoting relationships or connections.
    • Application: Enables the study of social structures, information diffusion, and community detection within social groups.
  6. Program Call Graph and Variable Dependency Graph:
    • Representation: Functions or procedures are nodes, and edges indicate calls or dependencies.
    • Application: Aids in understanding and optimizing software by visualizing the flow of program execution and dependencies between variables.

Graph Representation: 

Graphs can be represented in two main ways: using an adjacency matrix or an adjacency list: 

Traversing a Graph: 

Traversing a graph is a fundamental operation in graph theory and computer science. It involves visiting all the vertices (nodes) and edges (connections) of a graph in a systematic manner. There are two main methods for graph traversal: depth-first traversal and breadth-first traversal.

  1. Depth-First Traversal (DFS):
    • In DFS, you start at an initial vertex and explore as far as possible along each branch before backtracking.
    • This can be implemented using recursion or a stack data structure.
    • DFS is often used to find paths, cycles, and connected components in a graph.

Here’s a simple pseudocode for DFS:

function DFS(graph, start_vertex):
    if start_vertex is not visited:
        mark start_vertex as visited
        for each neighbor in graph[start_vertex]:
            if neighbor is not visited:
                DFS(graph, neighbor)
  1. Breadth-First Traversal (BFS):
    • In BFS, you start at an initial vertex and explore all its neighbors before moving on to their neighbors.
    • This is typically implemented using a queue data structure.
    • BFS is often used to find the shortest path or to explore the graph level by level.

Here’s a simple pseudocode for BFS:

function BFS(graph, start_vertex):
    create an empty queue
    enqueue start_vertex into the queue
    mark start_vertex as visited

    while the queue is not empty:
        current_vertex = dequeue from the queue
        for each neighbor in graph[current_vertex]:
            if neighbor is not visited:
                enqueue neighbor into the queue
                mark neighbor as visited

Applications of Graphs: 

Graphs have a wide range of applications, including:

  • Social networks
  • Transportation systems
  • Recommendation systems
  • Network analysis
  • And many more 

Graph Algorithms : 

Various graph algorithms are used for solving specific problems.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription