Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Graph

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Introduction to graph

A Graph is a non-linear data structure consisting of vertices and edges.


The vertices are sometimes also referred to as nodes and the edges are
lines or arcs that connect any two nodes in the graph. More formally
a graph is composed of a set of vertices ( V ) and a set of edges ( E ).
The graph is denoted by G (V, E).

Graph data structures are a powerful tool for representing and


analyzing complex relationships between objects or entities. They are
particularly useful in fields such as social network analysis,
recommendation systems, and computer networks. In the field of sports
data science, graph data structures can be used to analyze and
understand the dynamics of team performance and player interactions
on the field.

basic graph terminologies:


Graph: A graph is a mathematical structure that consists of a set of
vertices (or nodes) connected by edges (or links).

Vertex (Node): A vertex is a fundamental unit of a graph,


representing a point or an element.

Edge (Link): An edge is a connection between two vertices. It can be


directed or undirected, indicating the flow or lack of flow between
vertices.
Directed Graph (Digraph): In a directed graph, edges have a
direction, meaning they go from one vertex to another, but not
necessarily the other way around.

Undirected Graph: In an undirected graph, edges have no direction;


they simply represent a connection between two vertices.

Weighted Graph: A weighted graph assigns a numerical value


(weight) to each edge, indicating some kind of measurement, such as
distance, cost, or capacity.

Degree of a Vertex: The degree of a vertex is the number of edges


connected to it. In a directed graph, there can be both in-degree
(incoming edges) and out-degree (outgoing edges).

Path: A path is a sequence of vertices in which each adjacent pair is


connected by an edge.

Cycle: A cycle is a path that starts and ends at the same vertex, with no
repeated vertices or edges in between.

Connected Graph: A graph is considered connected if there is a path


between any two vertices.
Disconnected Graph: A graph is disconnected if there are one or
more pairs of vertices with no path between them.

Subgraph: A subgraph is a graph formed by selecting a subset of


vertices and edges from a larger graph.

Graph Representation: Graphs can be represented using various


data structures, including adjacency matrices and adjacency lists.

Relation of graph
Sequential representation,
A "sequential representation" is not a standard term in graph theory.
However, if you meant to refer to representing a graph using a sequential
or linear data structure, one common approach is an "adjacency list."
Adjacency List:
In an adjacency list representation, you store the graph as a collection of
lists or arrays. Each vertex is associated with a list that contains its
neighboring vertices. This is a space-efficient way to represent graphs,
especially when the graph is sparse (has relatively few edges).

example:
Suppose you have a graph with three vertices and the following edges:
Vertex A is connected to B and C.
Vertex B is connected to A.
Vertex C is connected to A.
You could represent this graph using an adjacency list as follows:

A: [B, C]
B: [A]
C: [A]
Each vertex (A, B, C) is associated with a list of its neighboring vertices.
This representation makes it easy to find the neighbors of any given
vertex and is efficient in terms of memory usage for sparse graphs.

If you meant something different by "sequential representation," please


provide more context or clarify your question, and I'd be happy to help
further
Linked representation of graph
A linked representation of a graph is a way to represent a graph using
data structures like linked lists or adjacency lists. This method is
commonly used when the graph is relatively sparse and memory
efficiency is a concern. Here's how it works:

Adjacency List:
Each vertex in the graph is represented as a node in a linked list.
For each vertex, you create a linked list (or an array of linked lists) to
store its neighboring vertices.
Each node in the linked list contains the identifier of the neighboring
vertex and, optionally, any edge weights or other relevant information.

Example:
Let's consider a simple undirected graph with three vertices A, B, and C
and the following edges:

A is connected to B and C.
B is connected to A.
C is connected to A.
The linked representation of this graph would look like this:

A -> B -> C
B -> A
C -> A
Here, each vertex has a linked list of its adjacent vertices. For instance,
the linked list associated with vertex A contains B and C, indicating that
A is connected to both B and C.

Adjacency Matrix of directed and undirected graph


An adjacency matrix is a way to represent a graph using a two-
dimensional array. In this matrix, each row and column represent a
vertex, and the entries in the matrix indicate whether an edge exists
between the corresponding vertices. The type of graph (directed or
undirected) affects the structure of the adjacency matrix. Here's how
they differ:

Undirected Graph:
In an adjacency matrix for an undirected graph, the matrix is symmetric
because the connection between two vertices A and B is the same as the
connection between B and A. If there's an edge between A and B, it's
also represented in the matrix for B and A.

Example for an undirected graph with vertices A, B, and C and edges A-


B, B-C:

A B C
A 0 1 0
B 1 0 1
C 0 1 0

Directed Graph :

In an adjacency matrix for a directed graph, the matrix is not necessarily


symmetric because the direction of the edges matters. If there's a
directed edge from A to B, it's represented in the matrix, but not
necessarily in the opposite direction from B to A.

Example for a directed graph with vertices A, B, and C and edges A ->
B, B -> C:

A B C
A 0 1 0
B 0 0 1
C 0 0 0
In this example, there is an edge from A to B (A -> B) and from B to C
(B -> C).

Key points about adjacency matrices:

The diagonal entries (e.g., A-A, B-B, C-C) are typically 0 in simple
graphs because a vertex is not connected to itself.
The values in the matrix can be 1 (indicating an edge) or 0 (indicating no
edge).
For weighted graphs, the matrix can contain weights instead of 0s and
1s.
Adjacency matrices are useful for various graph algorithms and
operations, and their structure depends on whether the graph is directed
or undirected.

Traversal used for graph

Breadth first search


Breadth First Search (BFS) is a graph traversal algorithm used to explore
all the vertices of a graph in a breadthward motion. It starts at a given
source vertex and explores its neighbors before moving on to their
neighbors. BFS is often used to find the shortest path between two
vertices in an unweighted graph.

Here's a high-level description of the BFS algorithm:

Start at the source vertex.


Enqueue the source vertex into a queue data structure.
While the queue is not empty:
a. Dequeue a vertex from the queue.
b. Process the vertex (e.g., mark it as visited or perform some specific
operation).
c. Enqueue all unvisited neighbors of the vertex into the queue.
Repeat steps 3 until the queue is empty or until a specific condition is
met.
BFS ensures that vertices are visited in increasing order of their distance
from the source vertex. It's commonly used for tasks like finding the
shortest path in unweighted graphs, checking for graph connectivity, and
more.

Depth First Search


Depth First Search (DFS) is a fundamental graph traversal algorithm
used to explore and navigate through graphs and trees. It starts at the
root node (or any arbitrary node in the case of a graph) and explores as
far as possible along each branch before backtracking. Here's a basic
outline of how DFS works:

Start at the initial node.


Explore one of its unvisited neighbors.
If an unvisited neighbor is found, move to that neighbor and repeat step
2.
If no unvisited neighbors are left, backtrack to the previous node and
explore other unvisited neighbors if available.
Repeat steps 2-4 until all nodes have been visited, or a specific node or
condition is reached.
DFS can be implemented using either recursion or an explicit stack data
structure to keep track of nodes to visit. It's used in various applications
like pathfinding, topological sorting, and solving problems related to
connected components.
Is there a specific aspect or application of DFS you'd like to learn more
about?

You might also like