Sunteți pe pagina 1din 3

Graph

Graph consists of a set of nodes that are connected by various edges. If the edges do not
show any direction, then the graph is called undirected graph, if the edges indicate direction
then the graph is called directed graph.

A graph G (V, E) is composed of, V: a finite set of vertices, E: a finite set of edges, such that
the edges are used to connect the vertices in V.

Example: V={a,b,c,d,e}, E={(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),(d,e)}



Weighted graph: In a weighted graph, each edge is assigned a weight value.

Directed graph: In a directed graph, an edge goes from one vertex, the source, to another,
the target, and hence makes connection in only one direction.




Undirected graph: Edges have no direction



Representation of graph: Graphs can be represented in two ways: Adjacency matrix and
Adjacency list.

Adjacency matrix: A structure for representing a graph in which, the presence of edges
between nodes is indicated by an entry, say 1, and the absence of edges between nodes is
indicated by an entry, say 0.

Representation of graph in a matrix
An adjacency-matrix representation of a graph is a 2-dimensional V x V array.
Each element in the array stores a Boolean value saying whether the edge is in the
graph.
The amount of space required to store an adjacency-matrix is O (V*V).
Any edge can be accessed, added, or removed in O (1) time.




Adjacency list: A structure for representing a graph, in which the edges are stored as lists
of connections between nodes.

Representation of graph in a list
An adjacency-list representation of a graph stores an out-edge sequence for each
vertex.
For sparse graphs, i.e., graphs with very few edges, this representation saves lot of
space.
The edges for each vertex can be accessed more efficiently.


Undirected Graph Adjacency List


Graph traversal techniques: There are two types for traversing the graph: Breadth First
Search (BFS), Depth First Search (DFS). When traversing the graph, these methods ensure
that every vertex and every edge is visited at least once in some well-defined order.

Breadth First search
Traversing a graph by visiting all the nodes attached directly to a starting node, first.
Breadth-first search is useful to find the shortest paths.
It is one of the simplest algorithms for searching a graph.
The algorithm works on both directed and undirected graphs.
It can be used to compute the distance (smallest number of edges) from source vertex
to each reachable vertex.

Method of implementation
It is implemented using queue.
Once a vertex is discovered, it is placed on a queue.
Since we process these vertices in first- in, first-out order, the oldest vertices are
expanded first, which are exactly those closest to the root.
Once the queue is empty, we have traversed through all vertices.

Depth-first search
Traversing a graph by visiting all the nodes lying on the path and exploring the path till
the end, before considering any other node.
DFS uses the backtracking technique.
The algorithm works on both directed and undirected graphs.

Method of implementation
It is implemented using stack.
Algorithm starts at a specific vertex, which becomes current vertex. The current vertex
is pushed into the stack.
Then algorithm traverse graph by any edge incident to the current vertex.
If the edge leads to an already visited vertex, then we backtrack to current vertex.
If, on other hand, edge leads to an unvisited vertex, then we go to it and that becomes
our current vertex. We proceed in this manner until we reach the very end of the path.
At this point, we start backtracking. We pop the element from the stack and the
popped element becomes the next node to be considered.
The process terminates when backtracking leads back to the start vertex, i.e., when
stack becomes empty and all the paths are explored.

Glossary
Vertex : A node in a graph.
Edge : A connection between two vertices of a graph; also known as arc.
Out-edge : Any directed edge going out of a node.
In-edge : Any directed edge coming into a node.
Adjacency : If a graph contains an edge (u, v), joining the node u to v, then v is said
to be adjacent to u.
Path : There exists a path from the node u to the node v, if v is reachable from
u, following certain edges.
Cycle : A cycle is a path that includes the same vertex two or more times. A
graph without cycles is acyclic.
DAG : Directed acyclic graph. It is a directed graph without cycles.

S-ar putea să vă placă și