+ All Categories
Home > Documents > Unit 5 Final

Unit 5 Final

Date post: 11-Nov-2015
Category:
Upload: lalith-kartikeya
View: 220 times
Download: 0 times
Share this document with a friend
Description:
Unit 5 Final
Popular Tags:
43
DATA STRUCTURE USING C++ Algorithm and Design — UNIT 5— SACHIN KUMAR SAXENA ASST. PROF. DEPARTMENT OF INFORMATION TECHNOLOGY COLLEGE OF ENGINEERING ROORKEE, ROORKEE [email protected] www.sachinplacement.blogspot.com Saturday, March 26, 202 2 Sachin Kumar Saxena, IT Dept. COER, Roorkee
Transcript
  • DATA STRUCTURE USING C++ Algorithm and Design

    UNIT 5SACHIN KUMAR SAXENAASST. PROF. DEPARTMENT OF INFORMATION TECHNOLOGYCOLLEGE OF ENGINEERING ROORKEE, [email protected]*Sachin Kumar Saxena, IT Dept. COER, Roorkee

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Graphs as one of the most important non-linear data structures

    The representation that models various kinds of graphs

    Some useful graph algorithms

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • A graph G is a discrete structure consisting of nodes (vertices) and the lines joining the nodes (edges) For finite graphs, V and E are finite. We can write a graph as G = (V, E)

    Graph traversals Visiting all the vertices and edges in a systematic fashion is called as a graph traversal*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Graphs are classified as directed and undirected graphs

    In an undirected graph, an edge is a set of two vertices where order does not make any relevance, whereas in a directed graph, an edge is an ordered pair*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Graph

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee* create() :Graphinsert vertex(Graph, v) :Graphdelete vertex(Graph, v) :Graphinsert edge(Graph, u, v) :Graphdelete edge(Graph, u, v) :Graphis empty(Graph) :Boolean;end graph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Adjacency matrix (sequential representation)Adjacency list (linked representation) Adjacency MultilistInverse Adjacency List*Sachin Kumar Saxena, IT Dept. COER, Roorkee*Representation of Graphs

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*A graph in which every edge is directed is called as a directed graph or diagraphA graph in which every edge is undirected is called as an undirected graphIf some of edges are directed and some are undirected in a graph, the graph is called as mixed graph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 1: Simple graph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Any graph which that contains parallel edges is called a MultigraphOn the other hand, a graph which has no parallel edges is called as a simple graph

    Figure 2: Multigraph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*A graph in which weights are assigned to every edge is called as a weighted graph

    Weights can also be assigned to vertices

    A graph of area and streets of a city may be assigned weights according to its traffic density

    A graph of areas and roads connecting may be assigned distance between cities to edges and area population to vertices

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*In a graph, a node which that is not adjacent to any other node is called an isolated nodeA graph containing only isolated nodes is called a null graphA graph in which weights are assigned to every edge is called as a weighted graph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 3: Null GraphFigure 4: Graph With Isolated Vertex

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • In a directed graph, for any node V the number of edges which that have V as their initial node is called outdegree of the node VIn other words, the number of edges incident from node is its outdegree (outgoing degree) and the number of edges incident to it is an indegree (incoming degree)The sum of indegree and out degree is the total degree of a node (vertex)In an undirected graph, the total degree or degree of a node is the number of edges incident with the node.The isolated vertex degree is zero

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*A path which originates and ends at the same node is called a cycle (circuit)A cycle is elementary if each node is traversed once (except origin) and is simple if every edge of cycle is traversed once

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Connectivity : A graph is said to be a connected one if and only if there exists a path between every pair of vertices

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 5 (a): Graph with two connected componentsFigure 5 (b): Graph with one connected componentFigure 5: Graph with connected component

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    A simple graph which that does not have any cycles is called acyclic graph

    Such graphs do not have any loops

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • The graphs represented using a sequential representation using matrices is called an adjacency matrix*Sachin Kumar Saxena, IT Dept. COER, Roorkee*Adjacency Matrix

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Adjacency Matrix representationFig a GraphFig b Matrix representation for graph shown in fig aMatrix

    Representation Figure 1: Adjacency Matrix representation

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • In an adjacency list, the n rows of the adjacency list are represented as n-linked lists, one list per vertex of the graphWe can represent G by an array Head, where Head[i] is a pointer to the adjacency list of vertex iEach node of the list has at least two fields: vertex and linkThe vertex field contains the vertexid, and link field stores the pointer to the next node storing another vertex adjacent to i*Sachin Kumar Saxena, IT Dept. COER, Roorkee*Adjacency Lists

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Fig : GraphFig : Adjacency List representation for graph shown in fig aFigure 2: Adjacency List representation

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Multiclass are lists where nodes may be shared among several other lists

    The node structure of such a list can be represented as follows :

    Figure 3: Node Structure for multilist

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 4: Representation for Adjacency multilist for graphFig a : GraphFig b: Multilist representation for graph shown in fig a

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Inverse adjacency lists is a set of lists that contain one list for vertex

    Each list contains a node per vertex adjacent to the vertex it represents

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 5: Representation for Graph and its inverse adjacency listFigure a: GraphFigure b: inverse Adjacency list Representation

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Graph TraversalsDepth-First SearchBreadth-First Search*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • DFS starts at the vertex v of G as a start vertex and v is marked as visitedThen, each unvisited vertex adjacent to v is searched using the DFS recursively Once all the vertices that can be reached from v have been visited, the search of v is completeIf some vertices remain unvisited, we select an unvisited vertex as a new start vertex and then repeat the process until all the vertices of G are marked visited*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 6:(a) Graph and its (b) adjacency list representation

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure a : Sample graphDepth-First Search RepresentationFigure b : Depth-First Search RepresentationThe set V = {1, 2, 3, 6, 5, 7, 8, 9, 4} represents the order in which they are visited. Hence, the DFS of the graph (Fig. a) gives the sequence as 1, 2, 3, 6, 5, 7, 8, 9, and 4. This is shown in Fig. bFigure 7: Representation for Depth-First Search for graph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • In BFS, all the unvisited vertices adjacent to i are visited after visiting the start vertex i and marking it visited

    Next, the unvisited vertices adjacent to these vertices are visited and so on until the entire graph has been traversed

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure 8: Breadth-first search sequence for the graphFigure a : Sample graphFigure b : Breadth-First Search Representation

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • A tree is a connected graph with no cycles

    A spanning tree is a sub-graph of G that has all vertices of G and is a tree

    A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • A tree is a connected graph with no cycles

    A spanning tree is a sub-graph of G that has all vertices of G and is a tree

    A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Figure. 9 Graph and spanning trees (a) Graph (b) Spanning tree (c) Minimum spanning tree

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • An undirected graph is connected if there is at least one path between every pair of vertices in the graph A connected component of a graph is a maximal connected sub-graph, that is, every vertex in a connected component is reachable from the vertices in the component

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Connected Components

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Following fig shows the Sample graph with one connected component

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Following fig shows the Graph with two connected componentsFigure. 10: Graph representation with connected Components Figure a Figure b

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Prims algorithm starts from one vertex and grows the rest of the tree by adding one vertex at a time, by adding the associated edges

    This algorithm builds a tree by iteratively adding edges until a minimal spanning tree is obtained, that is, when all nodes are added

    At each iteration, it adds to the current tree a vertex though minimum weight edge that does not complete a cycle*Sachin Kumar Saxena, IT Dept. COER, Roorkee*Prims Algorithm

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Minimum Spanning Tree

    Minimum Spanning TreeFigure a: Sample Graph Figure b : Minimum Spanning TreeFigure 11: representation of Minimum Spanning tree for given Graph

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Another way to construct a minimum spanning tree for a graph G is to start with a graph T = (V, E = ) consisting of the n vertices of G and having no edges

    In Prims algorithm, we start with one connected component, add a vertex to have one connected component and no cycles, and end up with one connected component

    Here, we start with n connected components; at each step, the number of connected components would reduce by one and end up with one connected component

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Kruskals Algorithm

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • The shortest path between two given vertices is the path having minimum length

    This problem can be solved by one of the greedy algorithms, by Edger W. Dijkstra, often called as Dijkstras algorithm

    *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Shortest Path Algorithm

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • *Sachin Kumar Saxena, IT Dept. COER, Roorkee*Shortest Path Algorithm

    Directed weighted graphShortest Path

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

  • Thank You !!!!!*Sachin Kumar Saxena, IT Dept. COER, Roorkee*

    Sachin Kumar Saxena, IT Dept. COER, Roorkee

    *Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee*Sachin Kumar Saxena, IT Dept. COER, RoorkeeSachin Kumar Saxena, IT Dept. COER, Roorkee


Recommended