Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views26 pages

Ds Mod 4

Uploaded by

xcr6p6ysq7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

Ds Mod 4

Uploaded by

xcr6p6ysq7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

‭Module 4‬

‭Introduction to Graphs‬

‭ raph‬ ‭is‬ ‭a‬ ‭non‬ ‭linear‬ ‭data‬ ‭structure.A‬‭graph‬‭contains‬‭a‬‭set‬‭of‬‭points‬‭known‬‭as‬‭nodes‬‭(or‬


G
‭vertices) and set of links known as edges (or Arcs) which connects the vertices.‬

‭ ‬‭graph‬‭is‬‭defined‬‭as‬‭Graph‬‭is‬‭a‬‭collection‬‭of‬‭vertices‬‭and‬‭arcs‬‭which‬‭connects‬‭vertices‬‭in‬
A
‭the‬‭graph.‬‭A‬‭graph‬‭G‬‭is‬‭represented‬‭as‬‭G‬‭=‬‭(‬‭V‬‭,‬‭E‬‭),‬‭where‬‭V‬‭is‬‭set‬‭of‬‭vertices‬‭and‬‭E‬‭is‬
‭set of edges.‬

‭Example: graph G can be defined as G = ( V , E ) Where V = {A,B,C,D,E} and‬

‭ = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.‬ ‭This is a graph with 5 vertices and‬


E
‭6 edges.‬

‭Graph Terminology‬

‭ .‬
1 ‭Vertex‬‭: An individual data element of a graph is‬‭called as Vertex. Vertex is‬
‭also known as node. In above example graph, A, B, C, D & E are known as‬
‭vertices.‬

‭ .‬
2 ‭Edge‬‭: An edge is a connecting link between two vertices.‬‭Edge is also known‬
‭as Arc. An edge is represented as (starting Vertex, ending Vertex).‬

‭3.‬ ‭Adjacent vertices:‬‭Two vertices are adjacent, if they‬‭are connected by an edge.‬

‭4.‬ ‭Self loop :‬‭Any edge that have only one vertex are‬‭called self loop.‬

‭ .‬
5 ‭Parallel edges:‬‭In some cases, two edges are present‬‭associated with‬
‭two vertex.These edges are called parallel edges.‬

‭6.‬ ‭Degree of vertex :‬‭Total number of edges connected‬‭to a vertex is said to be degree‬
‭of that vertex.‬
‭Indegree‬

‭Total number of incoming edges connected to a vertex is said to be indegree of that vertex.‬

‭Outdegree‬

‭Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.‬

‭7.‬ ‭Isolated vertex:‬‭A vertex with degree zero.‬

‭8.‬ ‭Pendant vertex:‬‭A vertex with degree one is called‬‭Pendant vertex.‬


‭9.‬‭Sub Graph :‬

‭ graph S is said to be a sub graph of a graph G if all the vertices and all the edges of S are‬
A
‭in G, and each edge of S has the same end vertices in S as in G. A subgraph of G is a‬
‭graph‬
‭G’ such that V(G’)‬‭⊆‬‭V(G) and E(G’)‬‭⊆‬‭E(G)‬

I‭n data structures, a subgraph refers to a subset of a larger graph. It consists of a subset‬
‭of vertices and edges taken from the original graph, maintaining the relationships‬
‭between the selected vertices. Subgraphs are commonly used to analyze specific parts‬
‭of a larger graph or to perform operations on a smaller portion of the data.‬

‭10‬ ‭Path:‬‭In a graph ,a path is a sequence of nodes in‬‭which each node is connected by‬
‭an edge to the next.‬

‭11‬ ‭Cycles :‬‭A cycle in a graph is a non empty trail in‬‭which only the first and last‬
‭vertices are equal.‬

‭ R A Cycle is a path that starts and ends at the same vertex, traversing a series of‬
O
‭edges without revisiting any vertex (except the starting vertex).‬

‭12Outgoing Edge‬
‭A directed edge is said to be outgoing edge on its orign vertex.‬

‭13 Incoming Edge‬

‭A directed edge is said to be incoming edge on its destination vertex.‬

‭TYPES OF GRAPH‬

‭1.‬‭Undirected Graph‬
‭A graph with only undirected edges is said to be undirected graph.‬
‭2.‬‭Directed Graph‬

‭A graph with only directed edges is said to be directed graph.‬

‭3.‬‭Complete Graph‬

‭ ‬‭graph‬‭in‬‭which‬‭any‬‭V‬‭node‬‭is‬‭adjacent‬‭to‬‭all‬‭other‬‭nodes‬‭present‬‭in‬‭the‬‭graph‬‭is‬‭known‬
A
‭as‬ ‭a‬ ‭complete‬ ‭graph.‬ ‭An‬ ‭undirected‬ ‭graph‬ ‭contains‬ ‭the‬ ‭edges‬ ‭that‬ ‭are‬ ‭equal‬ ‭to‬ ‭edges‬ ‭=‬
‭n(n-1)/2‬ ‭where‬ ‭n‬ ‭is‬ ‭the‬ ‭number‬ ‭of‬ ‭vertices‬ ‭present‬ ‭in‬ ‭the‬ ‭graph.‬ ‭The‬ ‭following‬ ‭figure‬
‭shows a complete graph. Or‬
‭A complete graph is a graph in which there is a unique edge connecting every pair of‬
‭distinct‬ ‭vertices.‬ ‭In‬‭a‬‭complete‬‭graph‬‭with‬‭n‬‭vertices,‬‭there‬ ‭are‬‭n.(n-1)/2‬ ‭edges,‬
‭ nd‬ ‭it‬ ‭is‬ ‭denoted‬ ‭by‬ ‭��‬‭K‭n‬ ‭,‬ ‬ ‭where‬ ‭n‬ ‭represents‬ ‭the‬ ‭number‬ ‭of‬ ‭vertices.‬
a
‭Complete‬ ‭graphs‬ ‭are‬ ‭characterized‬ ‭by‬ ‭their‬ ‭high‬ ‭level‬ ‭of‬ ‭connectivity,‬ ‭and‬ ‭they‬
‭are‬ ‭often‬ ‭used‬ ‭in‬ ‭graph‬ ‭theory‬ ‭as‬ ‭a‬ ‭reference‬ ‭point‬ ‭for‬ ‭certain‬ ‭properties‬ ‭or‬
‭algorithms.‬

‭4.‬‭Regular Graph‬

‭ regular graph is a graph in which each vertex has the same number of‬
A
‭neighbors, i.e., the same degree. If all vertices in the graph have the same‬

‭ egree, the graph is‬


d
‭said to be k-regular, where k is the degree of each vertex‬

‭5.‬‭Cyclic Graph‬

‭ ‬ ‭graph‬ ‭having‬ ‭cycle‬ ‭is‬ ‭called‬ ‭cyclic‬ ‭graph.‬ ‭In‬ ‭this‬ ‭case‬ ‭the‬‭first‬‭and‬‭last‬‭nodes‬‭are‬‭the‬
A
‭same. A closed simple path is a cycle.‬
‭6.‬‭Acyclic Graph‬

‭A graph without cycle is called acyclic graphs.‬

‭7 Simple Graph‬
‭A graph is said to be simple if there are no parallel and self-loop edges.‬

‭8 Connected Graph‬

‭ graph G is said to be connected if there is at least one path between every pair of‬
A
‭vertices in G. Otherwise,G is disconnected.‬
‭ connected graph G‬
A ‭A‬
‭disconnected graph G‬

‭ his graph is disconnected because the vertex v1 is not connected with the other vertices of‬
T
‭the graph.‬

‭9 Disconnected Graph‬
‭ ‬‭graph‬‭is‬‭disconnected‬‭if‬‭at‬‭least‬‭two‬‭vertices‬‭of‬‭the‬‭graph‬‭are‬‭not‬‭connected‬
A
‭by‬ ‭a‬ ‭path.‬ ‭If‬ ‭a‬ ‭graph‬ ‭G‬ ‭is‬ ‭disconnected,‬ ‭then‬ ‭every‬ ‭maximal‬ ‭connected‬
‭subgraph of G is called a connected component of the graph G.‬

‭Vertex 1‬ ‭Vertex 2‬ ‭PATH‬

‭a‬ ‭b‬ ‭a b‬

‭a‬ ‭c‬ ‭Not Available‬

‭a‬ ‭d‬ ‭Not Available‬

‭b‬ ‭c‬ ‭Not Available‬

‭c‬ ‭d‬ ‭c d‬

‭10.‬ ‭Bipartite Graph: A‬‭bipartite graph is a graph in which‬‭the vertices can‬


‭ e divided into two disjoint sets, such that no two vertices within the same set‬
b
‭are adjacent. In other words, it is a graph in which every edge connects a‬
‭vertex of one set to a vertex of the other set.‬
‭An alternate definition:‬‭Formally, a graph G = (V,‬‭E) is bipartite if and only if its‬
‭vertex set V can be partitioned into two non-empty subsets X and Y, such that‬
‭every edge in E has one endpoint in X and the other endpoint in Y. This‬
‭partition of vertices is also known as bi-partition.‬

‭Bipartite Graph‬

‭Note‬‭: In the above image nodes of the same colour‬‭belong to the same set.‬

‭ 1.‬ ‭Complete‬ ‭bipartite‬ ‭graph‬ ‭:‬ ‭Complete‬‭bipartite‬‭graph‬‭is‬‭a‬‭special‬‭type‬‭of‬


1
‭bipartite‬ ‭graph‬ ‭where‬ ‭every‬ ‭vertex‬ ‭of‬ ‭one‬ ‭set‬ ‭is‬ ‭connected‬ ‭to‬ ‭every‬ ‭vertex‬ ‭of‬ ‭other‬
‭set.‬

‭GRAPH REPRESENTATION‬

‭1.‬ ‭Adjacent Matrix‬


‭ he adjacency matrix in a graph is a matrix with rows and columns, represented by‬
T
‭graph vertices with 1 or 0 in the position‬‭𝑉‬‭𝑖𝑗‬ ‭according to whether‬‭𝑉‬‭𝑖‬ ‭or‬‭𝑉‬‭𝑗‬ ‭has a link or‬
‭ ot.‬‭In‬‭such‬‭a‬‭matrix‬‭when‬‭𝑖‭𝑡‬ℎ‬ ‭row‬‭and‬‭𝑗‭𝑡‬ℎ‬ ‭column‬‭element‬‭is‬‭1,‬‭then‬‭there‬‭will‬‭be‬‭an‬‭edge‬
n
‭between‬ ‭𝑖‬ ‭and‬ ‭𝑗‬‭vertex.‬‭When‬‭there‬‭is‬‭no‬‭edge,‬‭the‬‭value‬‭will‬‭be‬‭zero.‬‭The‬‭above‬‭graph‬
‭can be represented is as follows:‬

‭2.‬ ‭Adjacent List‬


‭ he‬‭adjacent‬‭list‬‭of‬‭a‬‭graph‬‭is‬‭linked‬‭with‬‭one‬‭list‬‭per‬‭vertex.‬‭It‬‭will‬‭have‬‭a‬‭head‬‭list‬
T
‭which‬ ‭contains‬ ‭all‬ ‭the‬ ‭vertices‬ ‭in‬ ‭a‬ ‭sequential‬ ‭order‬ ‭and‬ ‭each‬ ‭element‬ ‭in‬ ‭its‬ ‭linked‬ ‭list‬
‭represents‬ ‭the‬ ‭other‬ ‭vertices‬ ‭that‬ ‭form‬ ‭an‬ ‭edge‬ ‭with‬ ‭the‬ ‭vertex.‬ ‭The‬ ‭adjacency‬ ‭list‬
‭representation of above graph is as follows:‬
‭GRAPH TRAVERSAL METHODS‬

‭1.‬ ‭Depth First Search (DFS)‬


‭ his algorithm traverses a graph in a depth ward direction and uses a stack to‬
T
‭remember the next vertex to be visited when a dead end occurs i.e. for back tracking.‬

‭ tep 1:‬‭Define a stack of size with total number of‬‭vertices in the graph.‬
S
‭Step 2:‬‭Select any vertex as starting point for traversal‬‭visit that vertex and push it on to‬
‭the stack.‬
‭Step 3:‬‭Visit any one of the adjacent vertex of the‬‭vertex which at the top of stack and is‬
‭not visited and push it on to the stack.‬
‭Step 4:‬‭Repeat step 3 until there are no vertex to‬‭visit from vertices on the stack.‬
‭ tep 5:‬‭When there is no vertex to visit then use‬‭back tracking and pop one vertex from the‬
S
‭stack.‬
‭Step 6:‬‭Repeat steps 3, 4 and 5 until stack becomes‬‭empty.‬

‭Step 7:‬‭When stack become empty it will give the sequence‬‭of vertices visited‬
‭2.‬‭Breadth First Search (BFS)‬
‭This algorithm traverses a graph in breadth wise direction and is utilising a queue for processing‬
‭vertices.‬

‭ tep 1:‬‭Define a sequence of size with total number‬‭of vertices in a graph.‬


S
‭Step 2:‬‭Select any vertex as starting point for the‬‭traversal and visit that vertex. Insert the newly‬
‭selected vertex into the queue.‬
‭Step 3:‬‭Visit all adjacent vertex that vertex which‬‭is at the front of the queue and is not visited, insert that‬
‭vertex on to queue.‬
‭Step 4:‬‭Repeat step 3 until there is no new vertex‬‭to be visited from queue.‬
‭Step 5:‬‭When queue is empty an algorithm produces BFS sequence‬

You might also like