Find Saddle Point of a Matrix in Java - Javatpoint

文章推薦指數: 80 %
投票人數:10人

In a matrix, an element is called the saddle point that is the minimum (or smallest) element row-wise and maximum (or largest) element column-wise. The term ... ⇧SCROLLTOTOP Home Java Programs OOPs String Exception Multithreading Collections JavaFX JSP Spring SpringBoot Projects InterviewQuestions JavaTutorial WhatisJava HistoryofJava FeaturesofJava C++vsJava HelloJavaProgram ProgramInternal Howtosetpath? JDK,JREandJVM JVM:JavaVirtualMachine JavaVariables JavaDataTypes UnicodeSystem Operators Keywords ControlStatements JavaControlStatements JavaIf-else JavaSwitch JavaForLoop JavaWhileLoop JavaDoWhileLoop JavaBreak JavaContinue JavaComments JavaPrograms JavaObjectClass JavaOOPsConcepts NamingConvention ObjectandClass Method Constructor statickeyword thiskeyword JavaInheritance Inheritance(IS-A) Aggregation(HAS-A) JavaPolymorphism MethodOverloading MethodOverriding CovariantReturnType superkeyword InstanceInitializerblock finalkeyword RuntimePolymorphism DynamicBinding instanceofoperator JavaAbstraction Abstractclass Interface AbstractvsInterface JavaEncapsulation Package AccessModifiers Encapsulation JavaArray JavaArray JavaOOPsMisc Objectclass ObjectCloning Mathclass WrapperClass JavaRecursion CallByValue strictfpkeyword javadoctool CommandLineArg ObjectvsClass OverloadingvsOverriding JavaString JavaRegex ExceptionHandling JavaInnerclasses JavaMultithreading JavaI/O JavaNetworking JavaAWT&Events JavaSwing JavaFX JavaApplet JavaReflection JavaDate JavaConversion JavaCollection JavaJDBC JavaMisc JavaNewFeatures RMI Internationalization InterviewQuestions JavaMCQ next→ ←prev FindSaddlePointofaMatrixinJava Inthissection,wewillunderstandwhatissaddlepointinamatrix,andhowtofindthesaddlepointofamatrixthroughaJavaprogram. Whatisthesaddlepointinamatrix? Inamatrix,anelementiscalledthesaddlepointthatistheminimum(orsmallest)elementrow-wiseandmaximum(orlargest)elementcolumn-wise.Thetermsaddlepointiswidelyusedingametheory. Notethatamatrixmayhavemorethanone,andmaynothavesaddlepoints.Forexample,considerthefollowingmatrix. Intheabovematrix,10and8arethelowestintherespectivecolumnandhighestintherespectiverow.Hence,theabovematrixhastwosaddlepoints. Inthefollowingmatrix,thereisnosaddlepointexists. Algorithm Traverseeachrowofthematrixonebyoneandfindtheminimumelementsfromeachrow. Traversethesamecolumnforfindingthemaximumelementinwhichtheminimumelementisfound. Iftheminimumelementintherowandmaximumelementinthecolumnarethesame,theelementisthesaddlepointofthematrix. Iftheminimumelementintherowandmaximumelementinthecolumnarenotthesame,theredoesnotexistasaddlepoint. Repeattheabovestepsuntilwedonotgetthesaddlepoint. Howtofindsaddlepoint? Considerthefollowingmatrixandfindthesaddlepoint. Let'sfindthesaddlepoint. Row0:Minimuminrow0is4(atindex0)andmaximuminsamecolumn0is7.Bothelementsaredifferent,sorow0doesnotcontainanysaddlepoint. Row1:Minimuminrow1is7(atindex0)andmaximuminsamecolumn0is7.Bothelementsarethesame,so7isthesaddlepointofthematrix. Row2:Minimuminrow2is1(atindex1)andmaximuminsamecolumn1is8.Bothelementsaredifferent,sorow2doesnotcontainanysaddlepoint. JavaProgramtoFindtheSaddlePointinaMatrix FindSaddlePoint.java publicclassFindSaddlePoint { publicstaticvoidmain(Stringargs[]) { //initializingamatrix int[][]matrix={{1,2,3},{4,5,6},{7,8,9}}; intm=3,k; //variablestostoreminandmaxelements intmin=0,max=0; int[][]index=newint[3][3]; System.out.print("Thematrixis:\n"); inti,j; //printsthegivenmatrix //outerloopforrows for(i=0;i=matrix[i][j]) { min=matrix[i][j]; index[0][0]=i; index[0][1]=j; } } //findsthemaximumelementinthesamecolumn j=index[0][1]; max=matrix[0][j]; for(k=0;k



請為這篇文章評分?