Graphs Algorithms
Upcoming SlideShare
Loading in...5
×
 

Graphs Algorithms

on

  • 521 views

 

Statistics

Views

Total Views
521
Views on SlideShare
521
Embed Views
0

Actions

Likes
0
Downloads
21
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Graphs Algorithms Graphs Algorithms Presentation Transcript

  • Graph AlgorithmsKasun Ranga Wijeweera(Email: krw19870829@gmail.com)
  • What is a Graph?• Intuitively, a graph is a collection of vertices (or nodes) andthe connections between them• Generally, no restriction is imposed on the number of verticesin the graph or on the number of connections one vertex canhave to other vertices
  • A Simple Graph• A simple graph G = (V, E) consists of a nonempty set V ofvertices and a possibly empty set E of edges, each edge being aset of two vertices from V• |V| = Number of vertices• |E| = Number of edges View slide
  • A Directed Graph• A directed graph, or digraph, G = (V, E) consists of anonempty set V of vertices and a set E of edges (also calledarcs), where each edge is pair of vertices from V• The difference is that one edge of a simple graph is of theform {vi, vj}, and in this case, (vi, vj) != (vj , vi) View slide
  • A Multi Graph• Above definitions are restrictive in that they do not allow fortwo vertices to have more than one edge• A multi graph is graph in which two vertices can be joined bymultiple edges• Formal Definition: A multi graph G = (V, E, f) is composed ofa set of vertices V, a set of edges E, and a functionf: E{(vi, vj): (vi, vj in V) and (vi != vj)}
  • A Pseudo Graph• A pseudo graph is a multi graph with the condition vi != vjremoved, which allows for loops to occur• In a pseudo graph, a vertex can be joined with itself by an edge
  • A Path• A path from v1 to vn is a sequence of edges edge(v1v2),edge(v2v3), . . . , edge(vn-1vn) and is denoted as path v1, v2, v3, .. . , vn-1, vn• If v1 = v2 and no edge is repeated, then the path is called acircuit• If all vertices in a circuit are different, then it is called a cycle
  • A Weighted Graph• A graph is called a weighted graph if each edge has anassigned number• Depending on the context in which such graphs are used, thenumber assigned to an edge is called it weight, cost, distance,length, or some other name
  • A Complete Graph• A graph with n vertices is called complete and is denoted K n iffor each pair of distinct vertices there is exactly one edgeconnecting them• The number of edges in such a graph|E| = |V|*(|V| - 1)*0.5
  • A Sub Graph• A sub graph G’ of graph G = (V, E) is a graph (V’, E’) suchthat V’ is a subset of V and E’ is a subset of E• A sub graph induced by vertices V’ is a graph (V’, E’) suchthat and edge e in E if e in E’
  • Adjacent? Incident?• Two vertices vi and vj are called adjacent if the edge(vivj) is inE• Such an edge is called incident with the vertices vi and vj
  • The Degree of a Vertex• The degree of a vertex v, deg(v), is the number of edgesincident with v• If deg(v) = 0, then v is called an isolated vertex
  • Adjacency Matrix• An adjacency matrix of graph G = (V, E) is a binary |V|*|V|matrix such that each entry of this matrixa ij1; if there exists an edge(vivj)0; otherwise
  • Incidence Matrix• An incidence matrix of graph G = (V, E) is a |V|*|E| matrixsuch that each entry of this matrixa ij1; if edge ej is incident with vertex vi0; otherwise
  • Graph Traversals• Traversing a graph consists of visiting each vertex only onetime• Graphs may include cycles that can cause infinite loops• To prevent infinite loops each visited vertex can be marked toavoid revisiting it• Graphs can have isolated vertices• To visit those isolated vertices special mechanisms are needed• The Depth First Search algorithm is a well known algorithmfor traversing graphs
  • Depth First Search Algorithm• Each vertex v is visited and then each unvisited vertexadjacent to v is visited• If a vertex v has no adjacent vertices or all of its adjacentvertices have been visited, we backtrack to the predecessor ofv• The traversal is finished if this visiting and backtrackingprocess leads to the first vertex where the traversal started• If there is still some unvisited vertices in the graph, thetraversal continues restarting for one of the unvisited vertices• The algorithm assigns a unique number to each accessedvertex so that vertices are now renumbered
  • Depth First Search AlgorithmdepthFirstSearch()for all vertices vnum(v) = 0;edges = null;i = 1;while there is a vertex v such that num(v) is 0DFS(v);output edges;
  • Depth First Search AlgorithmDFS(v)num(v) = i++;for all vertices u adjacent to vif num(u) is 0attach edge(uv) to edges;DFS(u);
  • Reference
  • Any Questions?
  • Thank You!